背景
我使用squirrel
状态机并希望动态生成其相关类(状态、事件、上下文等)(状态可能会改变)
我尝试在 Springboot 应用程序启动后生成和编译 Java 代码。有四个文件A,B,C,D。
A,B,C 是简单的 java bean,D 扩展外部 lib 类。
示例代码如下所示:
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"));
}
它在 IDE(Intellij IDEA)中运行良好,但当我使用 command 运行它时不起作用java -jar
。异常消息是
A class "org.squirrelframework.foundation.fsm.impl.AbstractStateMachine" could not be found
使用的版本和依赖项:
1.8.0_202
IDE 和外部的Java- 弹簧靴
Greenwich.RELEASE
janino 依赖
<dependency>
<groupId>org.codehaus.janino</groupId>
<artifactId>janino</artifactId>
<version>3.1.0</version>
</dependency>
Maven构建输出
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] ------------------------------------------------------------------------
此外,我还尝试了 OpenHFT/Java-Runtime-Compiler,在 IDE 中运行良好,但也无法运行java -jar
:(
我搜索了许多与我相似的问题,但没有一个适合我的答案。也许我以错误的方式使用 janino,但我不知道如何解决。
非常感谢任何回复。
谢谢你,张