Backgroud
I use squirrel state machine and want to generate its relate class(state,event,context,etc.) dynamicly(state might change)
I tried to generate and compile Java code after Springboot application started. There are four files A,B,C,D.
A,B,C are simple java beans, D extends external lib class.
The example code is shown below:
public void testCompile() throws Exception {
String a = "package roa;\n" +
"public enum A{\n" +
" Pass,\n" +
" Refuse;\n" +
"}";
String b = "package roa;\n" +
"public enum B{\n" +
" Start,End;\n" +
"}";
String c = "package roa;\n" +
"public class C{}";
String d = "package roa;\n" +
"import org.squirrelframework.foundation.fsm.impl.AbstractStateMachine;\n" +
"public class D extends AbstractStateMachine<D,A,B,C> {}";
ICompilerFactory compilerFactory = CompilerFactoryFactory.getDefaultCompilerFactory();
ICompiler compiler = compilerFactory.newCompiler();
Map<String, byte[]> classes = Maps.newHashMap();
compiler.setClassFileCreator(new MapResourceCreator(classes));
compiler.setClassFileFinder(new MapResourceFinder(classes));
compiler.compile(new Resource[]{
new StringResource("roa/A.java", a),
new StringResource("roa/B.java", b),
new StringResource("roa/C.java", c),
new StringResource("roa/D.java", d)
});
ClassLoader cl = new ResourceFinderClassLoader(
new MapResourceFinder(classes),
ClassLoader.getSystemClassLoader()
);
System.out.println(cl.loadClass("roa.A"));
System.out.println(cl.loadClass("roa.B"));
System.out.println(cl.loadClass("roa.C"));
System.out.println(cl.loadClass("roa.D"));
}
It works well in IDE(Intellij IDEA),but won't work when I run it with command java -jar.
Exception message is
A class "org.squirrelframework.foundation.fsm.impl.AbstractStateMachine" could not be found
Version and Dependicies used:
- Java
1.8.0_202both in IDE and outside - Springboot
Greenwich.RELEASE
janino dependency
<dependency>
<groupId>org.codehaus.janino</groupId>
<artifactId>janino</artifactId>
<version>3.1.0</version>
</dependency>
maven build output
zhhang@bogon mvn clean install
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------< com.demo.cloud:oa >---------------------
[INFO] Building oa 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ oa ---
[INFO] Deleting /Users/zhhang/IdeaProjects/inno/oa/target
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ oa ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 17 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ oa ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 131 source files to /Users/zhhang/IdeaProjects/inno/oa/target/classes
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ oa ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/zhhang/IdeaProjects/inno/oa/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ oa ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ oa ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:3.1.1:jar (default-jar) @ oa ---
[INFO] Building jar: /Users/zhhang/IdeaProjects/inno/oa/target/oa-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.1.4.RELEASE:repackage (repackage) @ oa ---
[INFO] Replacing main artifact with repackaged archive
[INFO]
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ oa ---
[INFO] Installing /Users/zhhang/IdeaProjects/inno/oa/target/oa-1.0-SNAPSHOT.jar to /Users/zhhang/.m2/repository/com/demo/cloud/oa/1.0-SNAPSHOT/oa-1.0-SNAPSHOT.jar
[INFO] Installing /Users/zhhang/IdeaProjects/inno/oa/pom.xml to /Users/zhhang/.m2/repository/com/demo/cloud/oa/1.0-SNAPSHOT/oa-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9.087 s
[INFO] Finished at: 2020-03-10T10:57:48+08:00
[INFO] ------------------------------------------------------------------------
Besides,I also tried OpenHFT/Java-Runtime-Compiler,works well in IDE and not work run with java -jar as well :(
I've searched many questions that similar to mine,but no answer fit me well. Maybe I use janino in a wrong way,but I dont't know how to fix that.
Any reply is greatly appreciated.
Thank you,Zhhang