Java 使用spring启动应用程序在运行时编译类
我有两个gradle项目:Java 使用spring启动应用程序在运行时编译类,java,spring-boot,compilation,classloader,Java,Spring Boot,Compilation,Classloader,我有两个gradle项目: 定义一些业务实体的java项目 依赖于第一个项目的JavaSpringBootAPI Rest项目 API Rest项目公开了一个对某些Java代码进行运行时编译的POST端点 此Java代码扩展了一个类,该类的父级在业务项目中定义 代码编译是通过位于的InMemoryJavaCompiler完成的 根据应用程序的启动方式,会出现两种情况: 启动和调试API时,Rest使用IntelliJ工作正常。代码已编译并可以执行 使用java-jar启动和调试API Re
- 定义一些业务实体的java项目
- 依赖于第一个项目的JavaSpringBootAPI Rest项目
POST
端点
此Java代码扩展了一个类,该类的父级在业务项目中定义
代码编译是通过位于的InMemoryJavaCompiler
完成的
根据应用程序的启动方式,会出现两种情况:
- 启动和调试API时,Rest使用
工作正常。代码已编译并可以执行IntelliJ
- 使用
java-jar启动和调试API Rest时,它不起作用。jar与
任务打包在一起。Java可执行文件来自JDKbootJar
InMemoryJavaCompiler
类加载器看到,因此编译失败
堆栈跟踪示例
2019-01-19 10:33:50.955 ERROR 1636 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.mdkt.compiler.CompilationException: Unable to compile the source
[kind=ERROR, line=1, message=package com.foo.entities does not exist]
[kind=ERROR, line=1, message=cannot find symbol
symbol: class BaseClass]
[kind=ERROR, line=1, message=cannot find symbol
symbol: class Entity
location: class com.foo.myClass]
[kind=ERROR, line=1, message=method does not override or implement a method from a supertype]] with root cause
org.mdkt.compiler.CompilationException: Unable to compile the source
[kind=ERROR, line=1, message=package package com.foo.entities does not exist]
[kind=ERROR, line=1, message=cannot find symbol
symbol: class BaseClass]
[kind=ERROR, line=1, message=cannot find symbol
symbol: class Entity
location: class com.foo.myClass]
[kind=ERROR, line=1, message=method does not override or implement a method from a supertype]
at org.mdkt.compiler.InMemoryJavaCompiler.compileAll(InMemoryJavaCompiler.java:106) ~[InMemoryJavaCompiler-1.3.0.jar!/:na]
at org.mdkt.compiler.InMemoryJavaCompiler.compile(InMemoryJavaCompiler.java:126) ~[InMemoryJavaCompiler-1.3.0.jar!/:na]
Spring引导版本是2.1.2
,java是版本8
我试图编译的代码是这样的,在一行中,但在这里格式化以提高可读性
package com.foo;
import com.foo.entities.Entity;
public class myClass extends BaseClass {
@Override public void evaluate(Entity entity) {
System.out.println(entity);
}
}
当作为jar
打包和分发时,如何使这个API Rest工作
亲切问候。您是否使用JDK或JRE运行
java-jar
?编辑的OP是JDK