Skip to content

mrjameshamilton/jep457-hello-world

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JEP457 Hello World

JEP 457 Class-File API is a preview API to provide a standard API for parsing, generating, and transforming Java class files.

The example here shows how easy it is to use to create a new class that prints "Hello World".

ClassFile.of().buildTo(Path.of("HelloWorld.class"), of("HelloWorld"), classBuilder -> classBuilder
.withMethodBody("main", MethodTypeDesc.of(CD_void, of("java.lang.String").arrayType()), ACC_PUBLIC | ACC_STATIC, codeBuilder -> codeBuilder
.getstatic(of("java.lang.System"), "out", of("java.io.PrintStream"))
.ldc("Hello World")
.invokevirtual(of("java.io.PrintStream"), "println", MethodTypeDesc.of(CD_void, of("java.lang.Object")))
.return_()));

Building

You'll need JDK 22 and easiest way to install this on Linux is with SDK man:

sdk install java 22-open

If you have Java 22, you can then execute the run.sh script which will compile and execute Main.java. Main will create a Java class HelloWorld.class which will then be executed.

$ ./run.sh

Similar projects

There are many similar projects for parsing, generating, and transforming Java class files, including: