Start from command line
Of course, if you are an IDE junkie, you may prefer a visual way of getting started, but to prove that it is really just a matter of a few commands, let’s set everything up from a command line. Install Java, install Maven and invoke:
and that is it! Just three shell commands and you have all the environment you need!
Write Your Code!
Let’s open the
DataModel.java that contains the main logic and let’s add there
some Java code.
there are few methods annotated with
@Function annotation. They are ready to be invoked
when a button on the HTML page is pressed. Let’s modify one of them:
Of course you can put much more logic into the Java method - most of the core Java is supported. Now let’s try it. Rebuild and launch in a browser:
When the browser page is opened, press the
Start button and congratulations:
executed in a browser!
So Easy! Where’s the Catch?
Just a few simple steps and my Java code is running in the browser. Is everything so easy, or is there a hidden catch?
Of course, the transpiling has some limitations. It doesn’t support all Java APIs, but it shall be good enough for running Java algorithms in the browser and perform client side validations, computations, etc.
Moreover there is something really special: DukeScript project doesn’t provide the transpiling technology - we are just using it with the goal to make writing portable Java applications easy. As with any other system (Android, desktop, iOS) we always choose the best JVM available on the system. However, in case of plain browsers, there are two very good choices: Bck2Brwsr VM and TeaVM. With DukeScript infrastructure you can easily choose between any of them - e.g. no vendor lock-in and your choice to select the better JVM for your tasks. To try TeaVM just execute:
What is better: Bck2Brwsr or TeaVM?
The choice is yours. DukeScript projects is just proud to offer you two alternative transpiling solutions.
Packaging for Web
When your application is running, it is time to package it for the web. Again, it is easy. Execute following commands:
and you can see that a ZIP file has been created for you and is ready to be uploaded to your web server and used from your HTML pages. Alternatively you can do the same with TeaVM:
and upload or use the single
teavm.js file containing all your transpiled Java code.
Development and Testing
The power of DukeScript is in high portability of the applications. As such it is suggested to develop your application on desktop with a JavaFX WebView component - then all the goodies like no redeploys or easy Java and HTML debugging work. Once your application is running, you can then just transpile it.
Should there be any differences between desktop and transpiled version, the best is to write and execute a unit test - if it behaves differently between the standard Java on desktop and the one running in the browser, please contact our support and send us the test case - we are always ready to help. Read debugging transpiled Java article to learn more.
Enjoy Java in a browser done in the DukeScript way!