TeaVM es un compilador ahead-of-time que transforma bytecode de Java en JavaScript o WebAssembly, permitiendo ejecutar aplicaciones Java directamente en el navegador sin necesidad de reescribirlas en JavaScript.
Es útil para quienes quieren aprovechar su código Java (o incluso Kotlin y Scala) en entornos web modernos.
Es similar a lo que se hacía con GWT (Google Web Toolkit). Convertir tu código Java a código Javascript.
En este blog ya hemos hablado de WebAssembly:
Creando un proyecto Java (y TeaVM)
1. Creamos nuestro proyecto Java con Maven:
$ mvn -DarchetypeCatalog=local -DarchetypeGroupId=org.teavm -DarchetypeArtifactId=teavm-maven-webapp -DarchetypeVersion=0.13.1 archetype:generate $ cd demo-teavm
2. Una vez ubicados en el directorio creado modificaremos el programa principal ``MainClass.java``.
package com.inforhomex.web; import org.teavm.jso.dom.html.HTMLDocument; public class MainClass { public static void main(String[] args) { var document = HTMLDocument.current(); var div = document.createElement("div"); div.appendChild(document.createTextNode("TeaVM va a generar un elemento HTML.")); document.getBody().appendChild(div); } }
3. Modificaremos el archivo ``pom.xml``:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.inforhomex.web</groupId> <artifactId>demo-teavm</artifactId> <version>1</version> <packaging>war</packaging> <properties> <java.version>11</java.version> <teavm.version>0.13.1</teavm.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!-- Emulator of Java class library for TeaVM --> <dependency> <groupId>org.teavm</groupId> <artifactId>teavm-classlib</artifactId> <version>${teavm.version}</version> <scope>provided</scope> </dependency> <!-- JavaScriptObjects (JSO) - a JavaScript binding for TeaVM --> <dependency> <groupId>org.teavm</groupId> <artifactId>teavm-jso-apis</artifactId> <version>${teavm.version}</version> <scope>provided</scope> </dependency> <!-- Servlet specification --> <dependency> <groupId>jakarta.servlet</groupId> <artifactId>jakarta.servlet-api</artifactId> <version>6.0.0</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <!-- Configure Java compiler to use Java 8 syntax --> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.13.0</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> <!-- Configure WAR plugin to include JavaScript files generated by TeaVM --> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.4.0</version> <configuration> <webResources> <resource> <directory>${project.build.directory}/generated/js</directory> </resource> </webResources> </configuration> </plugin> <!-- Configure TeaVM --> <plugin> <groupId>org.teavm</groupId> <artifactId>teavm-maven-plugin</artifactId> <version>${teavm.version}</version> <executions> <execution> <id>web-client</id> <goals> <goal>compile</goal> </goals> <configuration> <!-- Directory where TeaVM should put generated files. This configuration conforms to the settings of the WAR plugin --> <targetDirectory>${project.build.directory}/generated/js/teavm</targetDirectory> <!-- Main class, containing static void main(String[]) --> <mainClass>com.inforhomex.web.MainClass</mainClass> <!-- Whether TeaVM should produce minified JavaScript. Can reduce JavaScript file size more than two times --> <minifying>true</minifying> <!-- Whether TeaVM should produce debug information for its built-in debugger --> <debugInformationGenerated>true</debugInformationGenerated> <!-- Whether TeaVM should produce source maps file --> <sourceMapsGenerated>true</sourceMapsGenerated> <!-- Whether TeaVM should also put source files into output directory, for compatibility with source maps --> <sourceFilesCopied>true</sourceFilesCopied> <!-- Optimization level. Valid values are: SIMPLE, ADVANCED, FULL --> <optimizationLevel>ADVANCED</optimizationLevel> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
4. Construimos el proyecto:
$ mvn clean package
5. Abrimos la página HTML generada en ``index.html``
<!DOCTYPE html> <html> <head> <title>Main page</title> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <script type="text/javascript" charset="utf-8" src="teavm/classes.js"></script> <script> </script> </head> <body onload="main()"> </body> </html>
Abrimos el archivo en un navegador. La salida será la siguiente:
TeaVM va a generar un elemento HTML.
¡Hemos generado código JS a través de TeaVM!
En próximas entregas continuaremos con este tema.
Enlaces:
https://teavm.org/https://webassembly.org/
https://onlyhaskellforyou.blogspot.com/2026/04/webassembly-con-haskell.html
https://codemonkeyjunior.blogspot.com/2026/04/webassembly-con-dlang.html
https://codemonkeyjunior.blogspot.com/2026/04/grain-un-lenguaje-para-webassembly.html



Comentarios
Publicar un comentario