Skip to content

Instantly share code, notes, and snippets.

@roryl
Last active June 6, 2016 12:55
Show Gist options
  • Save roryl/3580c74e7749ba8bd7e7d7d07278cc66 to your computer and use it in GitHub Desktop.
Save roryl/3580c74e7749ba8bd7e7d7d07278cc66 to your computer and use it in GitHub Desktop.
Lucee Java Compilation Example
component {
this.javaSettings = {LoadPaths = ["./"], loadCFMLClassPath = true, reloadOnChange= true, watchInterval = 2, watchExtensions = "jar,class,xml"}
function onRequestStart(){
compileClasses();
}
/**
* compiles all .java files in /WEB-INF/lucee/classes via the ECJ BatchCompiler
* lucee code added to classpath via the jars in System classpath and the latest rc patch
*/
function CompileClasses() {
var java = {
System : createObject( 'java', 'java.lang.System' )
, PrintWriter : createObject( 'java', 'java.io.PrintWriter' )
, StringWriter : createObject( 'java', 'java.io.StringWriter' )
, BatchCompiler : createObject( 'java', 'org.eclipse.jdt.core.compiler.batch.BatchCompiler', 'ecj-4.2.2.jar' )
};
var src = expandPath( '' );
var rc = arrayLast( directoryList( expandPath( '{lucee-server}/../patches' ) ) );
var cp = java.System.getProperty( "java.class.path" ); // all libs from System classpath
cp &= Server.separator.path & rc; // add the lucee-Core to classpath
// writeDump(cp);
// abort;
var out = java.StringWriter.init();
var outWriter = java.PrintWriter.init( out, true );
var cmd = "-cp #cp# #src#";
outWriter.println( "Compiler Command: " & cmd );
java.BatchCompiler.compile( cmd, outWriter, outWriter, javaCast( 'null', '' ) );
outWriter.println( "Done." );
var result = out.getBuffer().toString().trim();
writeDump(result);
return result;
}
}
public interface basic
{
public String sayHello();
}
component {
function sayHello(){
return "Hello this is a basic component";
}
}
public class Bicycle {
// the Bicycle class has
// three fields
public int cadence;
public int gear;
public int speed;
// the Bicycle class has
// one constructor
public Bicycle(int startCadence, int startSpeed, int startGear) {
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}
// the Bicycle class has
// four methods
public void setCadence(int newValue) {
cadence = newValue;
}
public void setGear(int newValue) {
gear = newValue;
}
public void applyBrake(int decrement) {
speed -= decrement;
}
public void speedUp(int increment) {
speed += increment;
}
}
This file has been truncated, but you can view the full file.
View raw

(Sorry about that, but we can’t show files that are this big right now.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment