Skip to content

Col-E/Simple-Memory-Compiler

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
src
 
 
 
 
 
 
 
 
 
 

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