konsoletyper / teavm

Compiles Java bytecode to JavaScript, WebAssembly and C
https://teavm.org
Apache License 2.0
2.64k stars 265 forks source link

Which Java APIs are supported? #947

Open jsfan3 opened 2 months ago

jsfan3 commented 2 months ago

I have tried to read the documentation, but I do not understand which Java APIs I can and cannot use.

For example, I understood that Java FX and Swing are not supported, but I can create my web application graphics using org.teavm.jso.dom.html.HTMLButtonElement, org.teavm.jso.dom.html.HTMLDocument, and org.teavm.jso.dom.html.HTMLElement. However, I could not find the relevant Javadocs.

In practice, I have to play with HTML elements and CSS styles for the graphical part, while Java is mainly useful for the client business logic. Please correct me if I am wrong.

Having said that, how do I know which standard Java classes to use?

konsoletyper commented 2 months ago

The link to set of supported classes from Java standard library (https://teavm.org/jcl-report/recent/jcl.html) is mentioned here: https://teavm.org/docs/runtime/java-classes.html

As for JSO, I publish both Javadocs and sources to Maven Central, which is requirement by Maven Central. By default, IDEA picks these and shows autocompletions, etc. But if for some reason you want them as separate jars, you can search on Maven Central for TeaVM artifacts of some certain version and download Javadocs. For example, for version 0.10.1, which is as for now is the latest, you can find them here: https://repo1.maven.org/maven2/org/teavm/teavm-jso-apis/0.10.1/

Please correct me if I am wrong.

In the practical cases, the gap between Java and JavaScript is not fully transparent, so you can have some issues here. So it depends. Personally, I write everything in Java, because I use TeaVM primarily to compile single app both to Android, iOS (Graal Native Image) and web, sharing 99.9% of code between platforms.

jsfan3 commented 2 months ago

Personally, I write everything in Java

So how do you build the GUI? Do you use the classes in the org.teavm.jso.dom.html package or do you use other classes?

I use TeaVM primarily to compile single app both to Android, iOS (Graal Native Image) and web

Could you share some more information on this point? How do you do it?

konsoletyper commented 2 months ago

So how do you build the GUI? Do you use the classes in the org.teavm.jso.dom.html package or do you use other classes?

I render everything using OpenGL, so it's either WebGL on the web, EGL on Android and MetalANGLE on iOS. Every OpenGL call is made though some platform-independant interface which has implementation over WebGL and EGL. Entire GUI is rendered by home-grown UI framework.

jsfan3 commented 2 months ago

Ok, but I don't have your framework, which if I understand correctly is your own private and not shared.

If I want to build a web application from scratch with TeaVM using what is publicly available, what tools do I have to build the GUI? This was the point of my previous question.

My HTML page is all here:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="MobileOptimized" content="width" />
    <meta name="HandheldFriendly" content="true" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="cleartype" content="on" />
    <title>TeaVM Project</title>
    <script type="text/javascript" charset="utf-8" src="classes.js"></script>
</head>
<body onload="main()">
</body>
</html>

Everything else I want to do with TeaVM. Here the classes.js is generated by TeaVM.

konsoletyper commented 2 months ago

There's a lot of choice. Of course, you can try to proceed with some JS framework (like React) to build UI and try to use TeaVM only to expose business logic. However, you should remember, that directly calling Java from JS and back is not possible, so you end up with wrappers and annotations. If interaction layer between Java and JS is limited, then it would be a working solution.

Another way is to do everything from Java. If you are ok to deal with raw DOM or you are ready to invent your own Java framework, then this would be a working solution. You can also try to find something, may be someone already created and published any Java framework for front-end development. For example, you can take a looks at following:

Otherwise, TeaVM is not for you. Despite I'd like to develop TeaVM ecosystem, I simply have no resources for it, so what I can do is to maintain compiler and to hope that someday someone implements a popular web framework on top of TeaVM.

jsfan3 commented 2 months ago

Thanks for the explanation and for your TeaVM project. Even maintaining a compiler from Java to Javascript is a big effort. Your project seems to be a great help for Java fans.

I will try to experiment with TeaVM by directly manipulating the raw DOM with the classes in the org.teavm.jso.dom.html package.

jsfan3 commented 1 month ago

I managed to create a nice and fully functional web application with TeaVM. Compiling from Java to JavaScript works great! For the graphics I manipulated the DOM directly from Java.

It is not clear to me what the WebAssembly compiling and especially the C compiling are for. Could you please give me an explanation?

konsoletyper commented 1 month ago

It is not clear to me what the WebAssembly compiling and especially the C compiling are for. Could you please give me an explanation?

It means literally: compile Java bytecode to WebAssembly and C. As for C I need some explanation. It can be used as a ultra-lightweight subset of GraalVM Native Image. The goal is to support platforms where it's hard to get into with LLVM. One may say that LLVM is supported everywhere, but it's not. It was for one project that we wanted to launch as WinRT/UWP application, and nobody could either patch OpenJDK to run as a WinRT app or to make RoboVM run under WinRT (and GraalVM Native Image was too fresh and unstable then). It turned out that for me it was easier to create C BE for TeaVM (especially, when there was WebAssembly BE that already implemented 99% of runtime magic, including GC) rather that for guys who tried alternative approaches. We managed to successfully compile our app (500KLOC of Java code) as a UWP app, but then the project was cancelled due to non-technical reasons. But C backend for TeaVM is still there and usable, because there was just no reason to drop it. This example, in addition to JS and WebAssembly can also target Linux (GTK)

jsfan3 commented 1 month ago

Thank you for your explanation. 500KLOC of Java code... wow!