Html5 development with Java using TeaVM

What is TeaVM

** TeaVM is a transpiler that converts Java to JavaScript / html. ** ** By using TeaVM, you will be able to develop html5 in Java.

A similar tool is Google's GWT (Google Web Toolkit), whereas GWT converts from source (.java) to JavaScript. TeaVM converts ByteCode (.class or .jar) to JavaScript. Therefore, TeaVM can be used from JVM languages other than Java. (There are sample projects of kotlin and Scala on the official page)

It also has better features such as reflection than GWT.

Official page

TeaVM URL http://teavm.org/ TeavVM source https://github.com/konsoletyper/teavm

JDK and Maven are required to use TeaVM, so please download / install it. Download JDK Download Maven Not required if it is already installed.

Creating a project

For details, refer to the Getting Started page on the official website.

** * It is not recommended for beginners of TeaVM to download the latest source from github and use it ** ** It's not stable, it can't be compiled, and the test is running all the time ** ** Initially we recommend creating a TeaVM project with Maven **

To create a TeaVM project with Maven, execute the following command on the command line as follows

> mvn -DarchetypeCatalog=local -DarchetypeGroupId=org.teavm -DarchetypeArtifactId=teavm-maven-webapp -DarchetypeVersion=0.6.1 archetype:generate

Specify for Maven-DarchetypeVersion version is For the latest version, please use the numbered version in the red box of the official Getting started image It may not be stable if it is too old or too new.

バージョンの画像

When you execute the command, you will be asked for the following items, so enter them. I think groupID can be group ID or package name artifactID specifies the project name. version remains the same As for pakage, the one with group ID is used, so leave it as it is Finally enter "Y" to create the project

バージョンの画像

After a while, a folder with the name specified by artifactID will be created in the current directory, and the project will be created in it.

Build the project

Move to the folder where the project was created (name specified by artifactID)

>mvn clean package

Then the first build will be done. ** Initially, it will download the required repositories, so it will take some time. ** ** I think it will take less than a minute to build from the second time onwards

The second and subsequent builds

>mvn package

It's okay.

Execution of generated JavaScript / html

The built file is target / (artificial ID)-(version) It will be created in the folder.

If you do not specify version and package, target / (artist ID)-1.0-SNAPSHOT I think it is generated in the folder

If you load index.html in the folder with a browser and the following display is displayed, the build is successful.

TeaVM generated element バージョンの画像

If you don't read the file, you can just load the html directly into your browser. However, if you want to read files or use XMLHttpRequest, you need to set up a server locally. (Because it becomes CORS)

Executable and required files

The only files needed to run are: No need for src etc

index.html teavm/classes.js teavm / runtime.js (may not be available)

If you need debug information, you will need the following files:

teavm/classes.js.map teavm/classes.js.teavmdbg

Edit

When you create a project src / main / java / package name /Client.java File has been created. Edit this file to play with the actual source.

Client.javaのソース

Hellow World !! Add a simple code. The text "Hello World !!" is displayed on the console. Like the image at the end of the main method

System.out.prinln("Hellow World !!");

Add the line.

HellowWorld

> mvn package

Build the package again and load index.html again in your browser. Then the JavaScript console should say "Hellow World !!"

コンソール

The JavaScript console depends on the browser. In Firefox, Menu → Tools → Web Development → Web Console (or CTRL + SHIFT + K or CTRL + SHIFT + I) For Chrome, click Menu → Other Tools → Developer Tools → Console tab (or CTRL + SHIFT + I)

Recommended Posts

Html5 development with Java using TeaVM
Game development with two people using java 2
Game development with two people using java 1
Game development with two people using java 3
Using Mapper with Java (Spring)
HTML parsing with JAVA (scraping)
[Java] Development with multiple files using package and import
Prepare Java development environment with Atom
Try using Redis with Java (jar)
Using Java with AWS Lambda-Eclipse Preparation
Using proxy service with Java crawling
Prepare Java development environment with VS Code
Using Java with AWS Lambda-Implementation-Check CloudWatch Arguments
Using Java with AWS Lambda-Implementation-Stop / Launch EC2
Using JupyterLab + Java with WSL on Windows 10
Java development with Codenvy: Hello World! Run
I tried using OpenCV with Java + Tomcat
Java development with Codenvy: Console app debug
Try using the Wii remote with Java
24 hours struggling with Android development, Java Optional
Java development training
[Java development] Java memory
Specify ClassPath when using jupyter + Java with WSL
Build a Java development environment with VS Code
Install java with Homebrew
Using Java 8 with Bluemix (on Liberty Runtime & DevOps Service)
Create a Java development environment using jenv on Mac
Build Java development environment with VS Code on Mac
Getting started with Java programs using Visual Studio Code
Java development environment memo
Install Java with Ansible
Using Pair with OpenJDK
Experienced Java users get started with Android application development
Comfortable download with JAVA
Switch java with direnv
Plugin development with ImageJ
Download Java with Ansible
Java development link summary
java development environment construction
Build Java development environment with WSL2 Docker VS Code
How to build Java development environment with VS Code
Let's scrape with Java! !!
[Development] Java framework comparison
[Environment construction] Build a Java development environment with VS Code!
Build Java with Wercker
Try to build a Java development environment using Docker
Build Java program development environment with Visual Studio Code
DSL development using ANTLR 4.7.1
Java + Spring development environment construction with VirtualBox + Ubuntu (Xfce4)
Endian conversion with JAVA
[Java / Kotlin] Escape (sanitize) HTML5 support with unbescape [Spring Boot]
I tried to create a java8 development environment with Chocolatey
Using multiple versions of Java with Brew on Mac + jEnv
Java web application development environment construction with VS Code (struts2)
I tried using Dapr in Java to facilitate microservice development
Socket communication with a web browser using Java and JavaScript ②
Socket communication with a web browser using Java and JavaScript ①
Domain Driven Development with Java and Spring Boot ~ Layers and Modules ~
I tried using the CameraX library with Android Java Fragment
Build WebAPP development environment with Java + Spring with Visual Studio Code
[Java development environment construction] Install OpenJDK 11 (Java 11) on macOS with Homebrew.