从 Java 运行 Ant 时设置 JAVA_HOME
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/652053/
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
Setting JAVA_HOME when running Ant from Java
提问by Adam Crume
The reason is long and boring, but I need to run an Ant script to compile Java 1.5 code from a Java 1.4 app. I keep getting this error, though:
原因很长很无聊,但我需要运行 Ant 脚本才能从 Java 1.4 应用程序编译 Java 1.5 代码。但是,我不断收到此错误:
BUILD FAILED
build.xml:16: Unable to find a javac compiler;
com.sun.tools.javac.Main is not on the classpath.
Perhaps JAVA_HOME does not point to the JDK.
It is currently set to "C:\j2sdk1.4.2_16\jre"
In my code, I have:
在我的代码中,我有:
Project p = new Project();
p.setUserProperty("ant.file", buildFile.getAbsolutePath());
p.setProperty("java.home", "C:\Program Files\Java\jdk1.6.0_04");
p.fireBuildStarted();
p.init();
// so on and so forth
but it ignores it. I've also tried p.setUserProperty(String, String), but that doesn't do the trick, either. Is there a way to do it without launching a separate process?
但它忽略了它。我也试过 p.setUserProperty(String, String),但这也不起作用。有没有办法在不启动单独的进程的情况下做到这一点?
回答by Jason Day
Does the javac task in your buildfile have fork="yes"? If not, then it doesn't matter what the java.homeproperty is set to; ant will attempt to call the javac Mainmethod in the same java process, which from your error is a JRE, not a JDK.
您的构建文件中的 javac 任务是否具有fork="yes"? 如果不是,那么java.home属性设置为什么并不重要;ant 将尝试Main在同一个 java 进程中调用 javac方法,从你的错误来看,它是一个 JRE,而不是一个 JDK。
EDITTry setting the executableproperty of your javac task to the full path to the javacbinary and add compiler="extJavac"to the task.
编辑尝试将executablejavac 任务的属性设置为javac二进制文件的完整路径并添加compiler="extJavac"到任务中。
回答by Maurice Perry
Shouldn't the backslashes be doubled?
反斜杠不应该加倍吗?
p.setProperty("java.home", "C:\Program Files\Java\jdk1.6.0_04");
回答by Real Red.
Have you set environment variables JAVA_HOME and ANT_HOME properly? If you are setting via code it should work though.
您是否正确设置了环境变量 JAVA_HOME 和 ANT_HOME?如果您通过代码进行设置,它应该可以工作。
Also check if your %JAVA_HOME%\bin directory %ANT_HOME%\bin should be in the environment variable 'path'.
还要检查您的 %JAVA_HOME%\bin 目录 %ANT_HOME%\bin 是否应该在环境变量“路径”中。
Your problem seems to be with the %JAVA_HOME%\bin not being present in the envt. variable path though.
您的问题似乎与 %JAVA_HOME%\bin 不存在于环境中。虽然可变路径。
回答by ricosrealm
Another way to make this work is to add 'tools.jar' to your classpath. The javac compiler is contained within this jar.
使这项工作的另一种方法是将“tools.jar”添加到您的类路径中。javac 编译器包含在这个 jar 中。
java -cp $JAVA_HOME/lib/tools.jar ...
java -cp $JAVA_HOME/lib/tools.jar ...
回答by Mangesh Jadhav
javac option is available in tools.jar. In eclipse, even if your JRE HOME points to a jdk, all the system libraries point to JDK_HOME\jre\lib. There is no tools.jar. You can add tools.jar as an external Jar file. This should solve your issue
javac 选项在tools.jar 中可用。在eclipse中,即使你的JRE HOME指向一个jdk,所有的系统库都指向JDK_HOME\jre\lib。没有tools.jar。您可以将 tools.jar 添加为外部 Jar 文件。这应该可以解决您的问题

