小编ron*_*nif的帖子

在 Spring Boot 应用程序中使用 JavaCompiler API - 无法正确设置编译类路径

我们有一个应用程序,它已从传统的 WAR Spring Web 应用程序迁移到我们希望成为现代 Spring Boot 可执行 Jar 的应用程序。

应用程序模块之一使用JavaCompilerAPI 生成 Java 代码并在运行时对其进行编译。
生成的代码需要驻留在 Web 应用程序类路径中的依赖项,因此我们大致有以下代码:

StandardJavaFileManager standardFileManager = compiler.getStandardFileManager(null, null, null);

List<String> optionList = new ArrayList<String>();

    // set compiler's classpath to be same as the runtime's
    String classpathString = System.getProperty("java.class.path");
    optionList.addAll(Arrays.asList("-nowarn",
                                    "-classpath",
                                    classpathString,
                                    "-g",
                                    "-source",
                                    javaVersion,
                                    "-target",
                                    javaVersion));

    Collection classpathFiles = getClasspathJars(classpathString);
    addPreviousCompiledClassesToClasspathFiles(rootClassPath, classpathFiles);

    try {
        File file = new File(getTargetRoot(tempClassPath));
        file.mkdirs();
        standardFileManager.setLocation(StandardLocation.CLASS_OUTPUT, Lists.newArrayList(file));
        standardFileManager.setLocation(StandardLocation.CLASS_PATH, classpathFiles);

        // adding current dir to the source path, to find …
Run Code Online (Sandbox Code Playgroud)

java-compiler-api spring-boot

5
推荐指数
1
解决办法
505
查看次数

标签 统计

java-compiler-api ×1

spring-boot ×1