java ToolProvider.getSystemJavaCompiler() 返回 null - 仅在安装 JRE 时可用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15513330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:52:42  来源:igfitidea点击:

ToolProvider.getSystemJavaCompiler() returns null - usable with only JRE installed?

javajavac

提问by Josh Sobel

I am trying to use the JavaCompilerclass:

我正在尝试使用JavaCompiler类:

When I call ToolProvider.getSystemJavaCompiler()it returns null.

当我调用ToolProvider.getSystemJavaCompiler() 时,它返回 null。

I think this is because I'm using a JRE instead of a JDK.

我认为这是因为我使用的是 JRE 而不是 JDK。

The problem is I want it to run on all platforms regardless of weather the user is using a JRE or a JDK.

问题是我希望它在所有平台上运行,而不管用户使用 JRE 或 JDK 的天气如何。

If anyone knows how to fix this, or an alternative method to use please comment.

如果有人知道如何解决此问题,或使用其他方法,请发表评论。

Any help would be appreciated.

任何帮助,将不胜感激。

回答by Akhilesh Dhar Dubey

ToolProvider.getSystemJavaCompiler() is not available.

ToolProvider.getSystemJavaCompiler() 不可用。

Is tools.jar missing from the classpath?

类路径中是否缺少 tools.jar?

Set class path to the tools.jarfile which can found in jdk\jre directory.

Set class path to the tools.jar可以在 jdk\jre 目录中找到的文件。

System.setProperty("java.home", "C:\\Program Files\\Java\\jdk1.7.0_02");

System.setProperty("java.home", "C:\\Program Files\\Java\\jdk1.7.0_02");

回答by George Van Treeck

Here is how to run the Java compiler from your application when there is no JDK installed.

以下是在未安装 JDK 的情况下如何从应用程序运行 Java 编译器的方法。

First, include the tools.jar file from a JDK with your Java app and put tools.jar in your classpath. Oracle probably won't like you doing that. But, there is legal work-around. You get the tools.jar file from the free JDKs offered by openjdk.org (openjdk), RedHat (IcedTea), or Azul Systems (Zulu).

首先,将来自 JDK 的 tools.jar 文件包含在您的 Java 应用程序中,并将 tools.jar 放在您的类路径中。Oracle 可能不喜欢你这样做。但是,有合法的解决方法。您可以从 openjdk.org (openjdk)、RedHat (IcedTea) 或 Azul Systems (Zulu) 提供的免费 JDK 中获取 tools.jar 文件。

Next, instead of using ToolProvider.getSystemJavaCompiler() and the JavaCompiler class, call the compiler located in tools.jar directly. Below is snippet of code:

接下来,不使用 ToolProvider.getSystemJavaCompiler() 和 JavaCompiler 类,而是直接调用位于 tools.jar 中的编译器。下面是代码片段:

String classpath = ...; // make sure tools.jar is in this path 
String sourcepath = ...; // path to your sources
String putputpath = ...; // directory for generated class files
String filepath = ...; // file path the file you want to compile

String[] args = new String[] {
"-classpath", classpath,
"-sourcepath", sourcepath,
"-d", putputpath,
filePath
};
com.sun.tools.javac.Main javac = new com.sun.tools.javac.Main();
int compileStatus = javac.compile(args);

回答by PSR

I think this is the problem.Explicitly specifying the version of java.exe you're using as the one in your JDK directory.

我认为这是问题所在。明确指定您正在使用的 java.exe 版本作为 JDK 目录中的版本。

see here for details

详情请看这里

回答by Mr. Suryaa Jha

Just copy tools.jar file from /lib to It works

只需将 tools.jar 文件从 /lib 复制到它的工作原理

You can obtain by System.out.println( System.getProperty( "java.home"))

可以通过 System.out.println( System.getProperty( "java.home")) 获取

Most of time it is like C:\Program files\Java\jre(version) [ for windows ]

大多数时候它就像 C:\Program files\Java\jre(version) [ for windows ]

回答by user3509406

Another solution is from: - http://bugs.java.com/bugdatabase/view_bug.do?bug_id=7181951

另一个解决方案来自: - http://bugs.java.com/bugdatabase/view_bug.do?bug_id=7181951

Copy tools.jar in JDK_HOME/lib/ into JRE_HOME/lib/. At least to me, it solved my issue magically!

将 JDK_HOME/lib/ 中的 tools.jar 复制到 JRE_HOME/lib/ 中。至少对我来说,它神奇地解决了我的问题!

(I did nothing recommended as above. I just copied it there.)

(我没有做上面推荐的任何事情。我只是在那里复制了它。)

回答by Arshad Ali

here is simple solution that worked for me

这是对我有用的简单解决方案

I just changed the jre System library to .....Program Files\Java\jdk1.7.0_55\jre instead of ....Program Files\Java\jdk1.7.0_55\bin and it worked for me.

我只是将 jre 系统库更改为 .....Program Files\Java\jdk1.7.0_55\jre 而不是 ....Program Files\Java\jdk1.7.0_55\bin 并且它对我有用。

回答by clankill3r

On a Mac this worked for me:

在 Mac 上,这对我有用:

  System.setProperty("java.home", "/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home");
  javax.tools.JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

(It does not work for idk 10 for some reason).

(出于某种原因,它不适用于 idk 10)。

回答by Manuel Musetti

I had both JRE and JDK in my buildPath...i just removed the JRE and it fixed.

我的 buildPath 中有 JRE 和 JDK...我刚刚删除了 JRE 并修复了它。