mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-22 08:14:09 -08:00
94 lines
3.6 KiB
Markdown
94 lines
3.6 KiB
Markdown
TeaVM
|
|
=====
|
|
|
|
What is TeaVM?
|
|
--------------
|
|
|
|
TeaVM is an ahead-of-time translator from Java bytecode to JVM.
|
|
It can be compared with GWT, however TeaVM does not require source code of your application and
|
|
all required libraries.
|
|
You can use TeaVM for building applications for the browser, due to the following features:
|
|
|
|
* per-method dependency analyzer, that determines a set of methods that are really needed
|
|
to run your application, so TeaVM won't translate whole JAR files;
|
|
* fast JavaScript; for now it is almost as fast as the JavaScript, generated by GWT;
|
|
* Java class library emulation;
|
|
* integration with Maven and Eclipse;
|
|
* generation of source maps;
|
|
* debugger;
|
|
* interoperation with JavaScript libraries together with the set of predefined browser interfaces.
|
|
|
|
How to use
|
|
----------
|
|
|
|
There are several options of using TeaVM. One is the Maven build. First, you write your code as if it were an
|
|
ordinary Java application:
|
|
|
|
```Java
|
|
package org.teavm.samples;
|
|
|
|
public class HelloWorld {
|
|
public static void main(String[] args) {
|
|
System.out.println("Hello, world!");
|
|
}
|
|
}
|
|
```
|
|
|
|
Second, you include the following plugin in your `pom.xml` build section:
|
|
|
|
```XML
|
|
<plugin>
|
|
<groupId>org.teavm</groupId>
|
|
<artifactId>teavm-maven-plugin</artifactId>
|
|
<version>0.2</version>
|
|
<dependencies>
|
|
<dependency>
|
|
<groupId>org.teavm</groupId>
|
|
<artifactId>teavm-classlib</artifactId>
|
|
<version>0.2</version>
|
|
</dependency>
|
|
</dependencies>
|
|
<executions>
|
|
<execution>
|
|
<id>generate-javascript</id>
|
|
<goals>
|
|
<goal>build-javascript</goal>
|
|
</goals>
|
|
<phase>process-classes</phase>
|
|
<configuration>
|
|
<minifying>true</minifying>
|
|
<mainClass>org.teavm.samples.HelloWorld</mainClass>
|
|
<mainPageIncluded>true</mainPageIncluded>
|
|
</configuration>
|
|
</execution>
|
|
</executions>
|
|
</plugin>
|
|
```
|
|
|
|
Now you can execute `mvn clean package` and get the generated JavaScript files in `target/javascript` folder.
|
|
Just open `target/javascript/main.html` page in your browser, open developer's console and press *Refresh* and
|
|
see what happen.
|
|
|
|
There is also the [teavm-samples](teavm-samples) module, containing examples of TeaVM-based projects.
|
|
|
|
DukeScript
|
|
----------
|
|
|
|
[DukeScript](http://wiki.apidesign.org/wiki/DukeScript) is a set of APIs that allows Java applications
|
|
easily talk to JavaScript environment to (usually) animate an HTML page. While DukeScript has its own
|
|
implementation of JVM, called [Bck2Brwsr](http://wiki.apidesign.org/wiki/Bck2Brwsr), TeaVM also provides
|
|
support for running DukeScript applications, using [teavm-html4j](teavm-html4j) plugin.
|
|
|
|
Live examples
|
|
-------------
|
|
|
|
Thanks to [Jaroslav Tulach](http://wiki.apidesign.org/wiki/User:JaroslavTulach), author of DukeScript, we have several
|
|
DukeScript example applications. One is the minesweeper game.
|
|
You can try its TeaVM-compiled version [here](http://xelfi.cz/minesweeper/teavm/), and then take a look at
|
|
[source code](http://source.apidesign.org/hg/html~demo/file/4dce5ea7e13a/minesweeper/src/main/java/org/apidesign/demo/minesweeper/MinesModel.java)
|
|
and [HTML page](http://source.apidesign.org/hg/html~demo/file/4dce5ea7e13a/minesweeper/src/main/webapp/pages/index.html).
|
|
|
|
Another example is avaialble [here](http://graphhopper.com/teavm/).
|
|
It uses [GraphHopper](https://github.com/graphhopper/graphhopper/) to build route in browser.
|
|
Unlike original GraphHopper example it works completely in browser instead of querying server.
|
|
Thanks to [Peter Karich](https://github.com/karussell). |