Skip to content

Col-E/Simple-Memory-Compiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple Memory Compiler

This is a basic wrapper for the javax.tools.JavaCompiler. As such, you will need to run this under a JDK. JRE's do not contain compilers.

  • For Java 8- this is found in tools.jar.
  • For Java 9+ this is found in the module java.compiler.

Features

  • Support for multiple source files / classes
  • Managable compiler flags
    • Wrappers for: classpath, target-version, debug-information, verbosity

Examples

Basic example:

// source code to compile
StringBuilder s = new StringBuilder();
s.append("public class HelloWorld {" +
         "  public static void main(String args[]) {" +
         "    A.print(\"Hello from an inner class\");" +
         "  }" +
         "  public static class A {" +
         "    public static void print(String s){" +
         "       System.out.println(s);" +
         "    }" +
         "  }" +
         "}");
// create the compiler, add the code
Compiler c = new JavaXCompiler();
c.addUnit("HelloWorld", s.toString());
c.compile();
// compiled code, note the additional inner class.
byte[] outer = c.getUnitCode("HelloWorld");
byte[] inner = c.getUnitCode("HelloWorld$A");
System.out.println(Arrays.toString(outer));
System.out.println(Arrays.toString(inner));

Specifying custom classpath:

Compiler c = ...
// by default the current runtime's 'java.class.path' is used
// you can specify additional paths like so:
c.setClassPath(Collections.singletonList("lib/MyLibrary.jar"));
// lib is a folder in the current directory

Targeting older versions of java:

Compiler c = ...
// by default it'll just use the current runtime version
c.setTarget(TargetVersion.V6);

Configuring debug information:

Compiler c = ...
// by default debug information is not included
// setting the booleans in debug will auto-gen the correct flags for attribute inclusion
c.getDebug().lineNumbers = true;

Using in your project

This project is hosted via JitPack.io. You can add this project to your maven project like so:

<repositories>
	<repository>
	    <id>jitpack.io</id>
	    <url>https://jitpack.io</url>
	</repository>
</repositories>
<dependencies>
	<dependency>
	    <groupId>com.github.Col-E</groupId>
	    <artifactId>Simple-Memory-Compiler</artifactId>
	    <version>2.2</version>
	</dependency>
</dependencies>

Building

Pre-built:

Build-yourself:

  • clone / download the repo
  • open a terminal in the directory with pom.xml
  • run mvn package
    • Generates jar file in /target directory