从 Java 运行 Ant 时设置 JAVA_HOME [英] Setting JAVA_HOME when running Ant from Java
问题描述
原因又长又无聊,但我需要运行 Ant 脚本才能从 Java 1.4 应用程序编译 Java 1.5 代码.不过,我不断收到此错误:
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:
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"
在我的代码中,我有:
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
但它忽略了它.我也试过 p.setUserProperty(String, String),但这也不起作用.有没有办法在不启动单独的进程的情况下做到这一点?
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?
推荐答案
你的构建文件中的 javac 任务有 fork="yes"
吗?如果不是,那么 java.home
属性设置为什么无关紧要;ant 将尝试在同一个 java 进程中调用 javac Main
方法,从你的错误来看,它是一个 JRE,而不是一个 JDK.
Does the javac task in your buildfile have fork="yes"
? If not, then it doesn't matter what the java.home
property is set to; ant will attempt to call the javac Main
method in the same java process, which from your error is a JRE, not a JDK.
EDIT 尝试将 javac 任务的 executable
属性设置为 javac
二进制文件的完整路径并添加 compiler="extJavac"
到任务.
EDIT Try setting the executable
property of your javac task to the full path to the javac
binary and add compiler="extJavac"
to the task.
这篇关于从 Java 运行 Ant 时设置 JAVA_HOME的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!