Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用spring启动应用程序在运行时编译类_Java_Spring Boot_Compilation_Classloader - Fatal编程技术网

Java 使用spring启动应用程序在运行时编译类

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

我有两个gradle项目:

  • 定义一些业务实体的java项目
  • 依赖于第一个项目的JavaSpringBootAPI Rest项目
API Rest项目公开了一个对某些Java代码进行运行时编译的
POST
端点

此Java代码扩展了一个类,该类的父级在业务项目中定义

代码编译是通过位于的
InMemoryJavaCompiler
完成的

根据应用程序的启动方式,会出现两种情况:

  • 启动和调试API时,Rest使用
    IntelliJ
    工作正常。代码已编译并可以执行

  • 使用
    java-jar启动和调试API Rest时,它不起作用。jar与
    bootJar
    任务打包在一起。Java可执行文件来自JDK

不工作意味着业务项目中定义的类在运行时不会被
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