days
-3
-9
hours
0
-3
minutes
0
-7
seconds
-4
-3
search
You don't have to learn JavaScript development stack

This tool will come in handy if you’re a Java developer who needs to write web front-end from scratch

Gabriela Motroc
Java

© Shutterstock / Kosev

TeaVM, an ahead-of-time compiler of Java bytecode to JavaScript might come in handy if you’re a Java developer who needs to write web front-end from scratch. However, if you want to get your large existing codebase in Java or Kotlin and produce JavaScript, you’ve come to the wrong place.

What does your toolkit look like, Java developers? Here’s one more tool that could come in handy if you need to write web front-end from scratch. TeaVM is an ahead-of-time compile of Java bytecode and it’s capable of emitting JavaScript and WebAssembly.

Although its “close relative” is GWT, the difference is that “TeaVM relies on existing compilers, be it javac, kotlinc or scalac. These compilers produce bytecode (*.class or *.jar files), then TeaVM takes this bytecode and produces JavaScript code,” according to the project description.

If you are a Java (or Kotlin, or Scala) developer who used to write back-end code, TeaVM might be your choice. It’s true that a good developer (including Java developer) can learn JavaScript. However, to become an expert you have to spend a reasonable amount of your time.

 

    DevOpsCon Whitepaper 2018

    Free: BRAND NEW DevOps Whitepaper 2018

    Learn about Containers,Continuous Delivery, DevOps Culture, Cloud Platforms & Security with articles by experts like Michiel Rook, Christoph Engelbert, Scott Sanders and many more.

You can use TeaVM if:

  • You are a Java developer and you are going to write web front-end from scratch.
  • You already have Java-based backend and want to integrate front-end code tightly into your existing development infrastructure.
  • You have some Java back-end code you want to reuse in front-end.
  • You are ready to rewrite your code to work with TeaVM.

SEE ALSO: Let it flow in Java

TeaVM benefits:

  • It tries to reconstruct the original structure of a method, so in most cases, it produces JavaScript that you would write manually. No bloated while/switch statements, as naive compilers often do.
  • It supports threads.
  • It is fast
  • It produces source maps; TeaVM IDEA plugin allows to debug code right from the IDE.

There’s also a subproject called Flavour.

What it is: a framework for writing single-paged web applications (you can use it together with your back-end code)

What it’s not: a server-side framework. You should write your back-end code using “normal” JDK like OpenJDK, Oracle JDK using your favorite frameworks.

The easiest way to run TeaVM is to use teavm-maven-plugin. Here’s an example

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.teavm</groupId>
        <artifactId>teavm-maven-plugin</artifactId>
        <version>0.5.1</version>
        <dependencies>
          <!-- This dependency is required by TeaVM to emulate subset of Java class library -->
          <dependency>
            <groupId>org.teavm</groupId>
            <artifactId>teavm-classlib</artifactId>
            <version>0.5.1</version>
          </dependency>
        </dependencies>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
            </goals>
            <phase>process-classes</phase>
            <configuration>
              <mainClass>org.teavm.samples.HelloWorld</mainClass>
              <mainPageIncluded>true</mainPageIncluded>
              <debugInformationGenerated>true</debugInformationGenerated>
              <sourceMapsGenerated>true</sourceMapsGenerated>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

If you want to learn how to get started with TeaVM, here’s how.

Author
Gabriela Motroc
Gabriela Motroc is editor of JAXenter.com and JAX Magazine. Before working at Software & Support Media Group, she studied International Communication Management at the Hague University of Applied Sciences.

Leave a Reply

Be the First to Comment!

avatar
400
  Subscribe  
Notify of