diff --git a/cli/src/main/java/de/mirkosertic/bytecoder/cli/BytecoderCLI.java b/cli/src/main/java/de/mirkosertic/bytecoder/cli/BytecoderCLI.java index 1d7f2ba79..7f0f6634a 100644 --- a/cli/src/main/java/de/mirkosertic/bytecoder/cli/BytecoderCLI.java +++ b/cli/src/main/java/de/mirkosertic/bytecoder/cli/BytecoderCLI.java @@ -34,6 +34,8 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; +import java.net.URL; +import java.net.URLClassLoader; public class BytecoderCLI { @@ -42,6 +44,9 @@ public class BytecoderCLI { @Command(name = "Bytecoder") public static class CLIOptions { + @Option(names = "-classpath", required = true, description = "Die Directory containing the JVM class files to be compiled.") + protected String classpath; + @Option(names = "-mainclass", required = true, description = "Name of the class that contains the main() method") protected String mainClass; @@ -73,7 +78,7 @@ public static class CLIOptions { protected boolean minifyCompileResult = true; } - public static void main(String[] args) throws IOException, ClassNotFoundException { + public static void main(final String[] args) throws IOException, ClassNotFoundException { final CLIOptions theCLIOptions = new CLIOptions(); try { @@ -93,9 +98,11 @@ public static void main(String[] args) throws IOException, ClassNotFoundExceptio theBytecoderDirectory.mkdirs(); final ClassLoader theLoader = BytecoderCLI.class.getClassLoader(); - final Class theTargetClass = theLoader.loadClass(theCLIOptions.mainClass); + final URLClassLoader classLoader = new URLClassLoader(new URL[] {new File(theCLIOptions.classpath).toURI().toURL()}, theLoader); + + final Class theTargetClass = classLoader.loadClass(theCLIOptions.mainClass); - final CompileTarget theCompileTarget = new CompileTarget(theLoader, CompileTarget.BackendType.valueOf(theCLIOptions.backend)); + final CompileTarget theCompileTarget = new CompileTarget(classLoader, CompileTarget.BackendType.valueOf(theCLIOptions.backend)); final BytecodeMethodSignature theSignature = new BytecodeMethodSignature(BytecodePrimitiveTypeRef.VOID, new BytecodeTypeRef[] { new BytecodeArrayTypeRef(BytecodeObjectTypeRef.fromRuntimeClass(String.class), 1) }); diff --git a/manual/README.md b/manual/README.md index b417911ab..1d51a6bf4 100644 --- a/manual/README.md +++ b/manual/README.md @@ -20,7 +20,68 @@ The *WebAssembly* backend transforms the intermediate representation into WebAss The *OpenCL* backend is used to compile single algorithms into OpenCL and execute them on the GPU. This backend is designed to enhance existing programs running on the JVM to utilize the vast power of modern GPUs. -## Hello World! +## Jump-Start using the Command-Line Interface + +Lets assume we have the following Java class, and we want to compile it to JavaScript and run it in the Browser: + +``` +package bytecodertest; + +public class HelloWorld { + + public static void main(String[] args) { + System.out.println("Hello World!"); + } +} +``` + +First of all, we need to compile the Java sources to a JVM class file. This is done by using the `javac` tool from the JDK: + +``` +javac HelloWorld.java +``` + +Now, we have the compiled .class files. Now we can use the Bytecoder CLI to compile it to JavaScript! + +Step 1: Download the CLI from Maven central: + +``` +wget http://central.maven.org/maven2/de/mirkosertic/bytecoder/bytecoder-cli/2019-06-13/bytecoder-cli-2019-06-13-executable.jar +``` + +Step 2: Invoke the CLI: + +``` +java -jar bytecoder-cli-2019-06-13-executable.jar -classpath=. -mainclass=bytecodertest.HelloWorld -builddirectory=. -backend=js -minify=false +``` + +Step 3: Create an embedding HTML document + +Now we have a `bytecoder.js` file, which needs to be embedded into a HTML document. Here is a sample +HTML file embedding the Bytecoder JavaScript and invoking it: + +``` + +
+