当前位置: 首页 > 知识库问答 >
问题:

在使用JavaCompiler从另一个应用程序中编译类文件后,如何运行它?

陆畅
2023-03-14

我目前有以下代码:

private void compile(){ 

        List<File> files = getListOfJavaFiles();

        //JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        //compiler.run(null, null, null, srcDirectory.getPath()+"/Main.java");

        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
           StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);

           Iterable<? extends JavaFileObject> compilationUnits1 =
               fileManager.getJavaFileObjectsFromFiles(files);

           List<String> optionList = new ArrayList<String>();
           // set compiler's classpath to be same as the runtime's
           optionList.addAll(Arrays.asList("-classpath",System.getProperty("java.class.path")));

           //need to add options here.
           compiler.getTask(null, fileManager, null, optionList, null, compilationUnits1).call();

           //compiler.run(null, null, null, srcDirectory.getPath()+"/Main.java");
          // fileManager.close();

    }

但是我现在正试图让它实际运行已经编译的文件。在控制台中没有看到此结果的输出,但是在我成功编译的main.java文件中(我可以看到。class文件),我已经放入了“system.out.println(”main class is running“);所以我希望在运行应用程序时看到这个结果。

共有1个答案

鲁光霁
2023-03-14

您可以创建一个URLClassLoader来加载新编译的类,或者您可以查看我编写的一个库,该库将在内存中编译,并在默认情况下加载到当前的类加载器中。

如果您已经生成了代码,它将在调试时将文件保存到源目录中,以便您可以单步执行生成的代码(否则它将在内存中执行所有操作)

这样只能加载一个类一次,所以如果需要加载多个版本,我建议您实现一个接口,并每次更改类的名称。

// this writes the file to disk only when debugging is enabled.
CachedCompiler cc = CompilerUtils.DEBUGGING ?
        new CachedCompiler(new File(parent, "src/test/java"), new File(parent, "target/compiled")) :
        CompilerUtils.CACHED_COMPILER;

String text = "generated test " + new Date();
Class fooBarTeeClass = cc.loadFromJava("eg.FooBarTee", "package eg;\n" +
    '\n' +
    "import eg.components.BarImpl;\n" +
    "import eg.components.TeeImpl;\n" +
    "import eg.components.Foo;\n" +
    '\n' +
    "public class FooBarTee{\n" +
    "    public final String name;\n" +
    "    public final TeeImpl tee;\n" +
    "    public final BarImpl bar;\n" +
    "    public final BarImpl copy;\n" +
    "    public final Foo foo;\n" +
    '\n' +
    "    public FooBarTee(String name) {\n" +
    "        // when viewing this file, ensure it is synchronised with the copy on disk.\n" +
    "        System.out.println(\"" + text + "\");\n" +
    "        this.name = name;\n" +
    '\n' +
    "        tee = new TeeImpl(\"test\");\n" +
    '\n' +
    "        bar = new BarImpl(tee, 55);\n" +
    '\n' +
    "        copy = new BarImpl(tee, 555);\n" +
    '\n' +
    "        // you should see the current date here after synchronisation.\n" +
    "        foo = new Foo(bar, copy, \"" + text + "\", 5);\n" +
    "    }\n" +
    '\n' +
    "    public void start() {\n" +
    "    }\n" +
    '\n' +
    "    public void stop() {\n" +
    "    }\n" +
    '\n' +
    "    public void close() {\n" +
    "        stop();\n" +
    '\n' +
    "    }\n" +
    "}\n");

// add a debug break point here and step into this method.
FooBarTee fooBarTee = new FooBarTee("test foo bar tee");
Foo foo = fooBarTee.foo;
assertNotNull(foo);
assertEquals(text, foo.s);
 类似资料:
  • 问题内容: 我有一个Main.java和Test.java类,它们要编译并在Test.java代码中运行Main.java。这是我的代码 我只在Main.java中打印“ ok”,但是此代码不打印任何内容。问题是什么 ? 问题答案: 我修改了代码以包括一些检查: 这是Main.java: 当一切都很好时,它就可以工作: 现在,例如,如果在Main.java中出现一些错误: 它仍然显示“ ok”,因

  • 问题内容: 我正在编写一个makefile,它将一个文件编译到另一个目录中,然后我想在不更改目录的情况下运行它。我想按照以下方式做一些事情: Java文件所在的位置,并且makefile不在那里运行。 我怎样才能做到这一点? 问题答案: 我可能会误解这个问题,但是您可以使用 这将在 然后,您可以通过在类路径上包含它来运行它。例如 如果要在其他目录中生成类文件,则可以使用选项。

  • null (如果可能的话,它应该在db中创建表并生成html文件。我看到了http://velocity.apache.org/,所以是否可以使用它生成java代码。)

  • 我有一个用Java 8编译的可执行jar文件,其中包含一个名为< code>app.jar的JavaFX应用程序。 当我安装了Java8时,我可以简单地用运行它。 现在我只安装了Java 11,运行上面的命令会产生以下错误: 因为Java 11不再包含JavaFX。 所以我考虑安装JavaFX。openjfx.org以两种形式提供JavaFx:SDK和JMOD文件。 我认为我不需要SDK,因为我不

  • 问题内容: 我正在编写用于Java学习的Web应用程序。使用哪些用户可以在我的服务器上编译其代码+运行该代码。使用JavaCompiler可以很容易地进行编译: 我设法用代码加载类: 我如何保护我的应用程序免遭无休止的循环和邪恶的学生;) 有什么办法可以终身运行该代码? 内存泄漏有任何风险吗,我该怎么解决。 这是一个好的解决方案,还是您可以提出更好的建议? 谢谢。齐姆 问题答案: 我如何保护我的应

  • 问题内容: 我有一个.jar文件,可以在命令行上运行: 我想将此.jar的输出另存为另一个Java程序内的String变量。 我该怎么做? 我尝试在程序中包含myFile.jar作为参考,并在程序中进行了操作。但这只是将结果打印到控制台,我无法在程序中使用结果。 希望这不会太令人困惑。 问题答案: 如果您不能包括另一个罐子, 你可以用这样的东西 我知道我的代码并不像捕获的异常那样完美,但是我认为这