本文介绍了JavaCompiler返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在沿着Codingbat.com创建程序。

I am creating a program along the lines of Codingbat.com.

在运行时,它需要编译代码,然后执行它。这已全部处理。
当前,我被迫使用JavacTool,它要求将其打包在一起。

During Runtime, it needs to compile code, and then execute it. This has all been handled.Currently, I am forced to use the JavacTool, which requires it to be packed alongside.

我有2个基本问题:

1)从可执行jar运行时,如何阻止ToolProvider.getSystemJavaCompiler()返回null?

1) How can I stop the ToolProvider.getSystemJavaCompiler() from returning null when ran from an executable jar?

2)如果以上无法,是否有办法添加com.sun.tools.javac.api.JavacTool的jar;

2) If the above is not possible, is there a way to add the jar of com.sun.tools.javac.api.JavacTool; without having it as a referenced library, so that it acts like a regular import?

感谢您回答这个问题,如果您愿意,我可以上传带有引用的Jar

Thanks for answering this, if you would like, I could upload the Jar with the referenced library, and the jar without it.

需要明确的是,带有引用库的那个可以工作,但是它变大了,并且比那个罐子慢通过eclipse运行,它使用JavaCompiler,而不是JavacTool

Just to be clear, the one with the referenced library works, but it is way to large, and slower then the jar that is ran through eclipse, that uses the JavaCompiler, not the JavacTool

谢谢

编辑:

我很确定这与Java一样可行,但我忘记了在哪里以及如何做。

I am pretty sure this is possible with java as I have seen it before, yet forget where and how.

推荐答案

怀疑,这只是您运行哪个版本的Java的问题。如果您运行JRE随附的版本,它将没有可用的工具。如果您运行JDK附带的版本,它将运行。

I suspect it's just a problem of which version of Java you run. If you run the version which comes with the JRE, it won't have the tools available. If you run the version which comes with the JDK, it will.

例如,下面是一个简短但完整的程序:

As an example, here's a short but complete program:

import javax.tools.*;

public class Test
{
    public static void main(String[] args)
    {
        System.out.println(ToolProvider.getSystemJavaCompiler());
    }
}

在JRE版本的java.exe上运行它我的笔记本电脑:

Running it with the JRE version of java.exe on my laptop:

c:\Users\Jon\Test>"\Program Files\java\jre7"\bin\java Test
null

现在使用JDK:

c:\Users\Jon\Test>"\Program Files\Java\jdk1.7.0"\bin\java Test
com.sun.tools.javac.api.JavacTool@441944ae

因此,请尝试明确指定与JDK相关的Java二进制文件。

So try explicitly specifying the a Java binary associated with the JDK.

这篇关于JavaCompiler返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 08:44