简体   繁体   English

以编程方式构建Netbeans平台应用程序时Ant类路径错误

[英]Ant class path error when programmatically building Netbeans platform application

I want to run a Netbeans 7.3 platform application (let's name it app A) from another Java application (app B). 我想从另一个Java应用程序(应用程序B)运行Netbeans 7.3平台应用程序(让我们将其命名为app A)。 To do this, in B's code I'm invoking the Ant library as follows: 为此,在B的代码中我按如下方式调用Ant库:

Path pathA = ... // where the A's sources are
Path fileBuild = pathA.resolve("build.xml");
Project p = new Project();
p.setUserProperty("ant.file", fileBuild.toFile().getAbsolutePath());
p.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
p.addReference("ant.projectHelper", helper);
helper.parse(p, buildFile);
p.setDefault("run");
p.executeTarget(p.getDefaultTarget());

Unfortunately, the error I'm getting is: 不幸的是,我得到的错误是:

C:\Program Files\NetBeans 7.3\harness\suite.xml:184: The following error occurred while executing this line:
C:\Program Files\NetBeans 7.3\harness\common.xml:217: 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:\Program Files (x86)\Java\jdk1.7.0_45\jre"
    at org.apache.tools.ant.ProjectHelper.addLocationToBuildException(ProjectHelper.java:568)
    at org.apache.tools.ant.taskdefs.Ant.execute(Ant.java:443)
    at org.apache.tools.ant.taskdefs.SubAnt.execute(SubAnt.java:306)
    at org.apache.tools.ant.taskdefs.SubAnt.execute(SubAnt.java:221)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
    at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.Target.execute(Target.java:435)
    at org.apache.tools.ant.Target.performTasks(Target.java:456)
    at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1393)
    at org.apache.tools.ant.Project.executeTarget(Project.java:1364)
...

The JAVA_HOME variable is set correctly both as system path variable and in netbeans config file and points to C:\\Program Files (x86)\\Java\\jdk1.7.0_45 . JAVA_HOME变量作为系统路径变量和netbeans配置文件正确设置,并指向C:\\Program Files (x86)\\Java\\jdk1.7.0_45

The question is, what am I missing? 问题是,我错过了什么? When running the app A from Netbeans menu, everything runs fine, so what should I append to the build file/ant settings to achieve A runs from B's code execution? 当从Netbeans菜单运行应用程序A时,一切运行正常,那么我应该追加到构建文件/ ant设置以实现从B的代码执行运行A?

Thanks in advance! 提前致谢!


Edit: I managed to get a more precise error stack, maybe this can help. 编辑:我设法得到一个更精确的错误堆栈,也许这可以帮助。

Error on test cases execution.
C:\Program Files\NetBeans 7.3\harness\common.xml:217: 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:\Program Files (x86)\Java\jdk1.7.0_45\jre"
    at org.apache.tools.ant.taskdefs.compilers.CompilerAdapterFactory.getCompiler(CompilerAdapterFactory.java:130)
    at org.apache.tools.ant.taskdefs.Javac.findSupportedFileExtensions(Javac.java:984)
    at org.apache.tools.ant.taskdefs.Javac.scanDir(Javac.java:961)
    at org.apache.tools.ant.taskdefs.Javac.execute(Javac.java:932)
    at org.netbeans.nbbuild.CustomJavac.execute(CustomJavac.java:105)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
    at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.Target.execute(Target.java:435)
    at org.apache.tools.ant.Target.performTasks(Target.java:456)
    at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1393)
    at org.apache.tools.ant.Project.executeTarget(Project.java:1364)
    at odoetest.server.ServerInvokerAnt.start(ServerInvokerAnt.java:48)
    at odoetest.EntryPoint.invokeTests(EntryPoint.java:58)
    at odoetest.EntryPoint.main(EntryPoint.java:38)

Edit-2: I don't have any separate ant installation and use the ant delivered with Netbeans. 编辑-2:我没有任何单独的ant安装,并使用Netbeans提供的蚂蚁。 When I run ant -diagnostics , I get this info . 当我运行ant -diagnostics ,我得到了这个信息 When I invoke the build or run job from the command line like 当我从命令行调用构建或运行作业时

ant -f /path/to/build.xml run

no errors are thrown, the program starts. 没有错误被抛出,程序启动。 I just can't do the exact same thing from java code from above. 我不能从上面的java代码做同样的事情。

Your Ant build.xml will still be referring to a JRE rather than the JDK. 您的Ant build.xml仍将引用JRE而不是JDK。

Right-click on your project, select Libraries , check that Java Platform points to a JDK rather than a JRE. 右键单击您的项目,选择Libraries ,检查Java Platform指向JDK而不是JRE。 See here for details. 详情请见此处

If this fails: 如果失败:

You can explicitly set properties for Project p as in manouti's answer above. 您可以在上面的manouti答案中明确设置Project p属性。 This will ensure that Project p as well as Project A will be referring to a JDK rather than JRE. 这将确保Project pProject A将引用JDK而不是JRE。 AFAIK, no need to fork. AFAIK,无需分叉。

You can set properties in your .properties file, though it is not always easy to debug whether this is being implemented for each project. 您可以在.properties文件中设置属性,但调试是否为每个项目实现这些属性并不总是很容易。

You can explicitly include folders/files on the classpath of your target using the <classpath> element - see the Apache Ant Manual for some not very clear documentation. 您可以使用<classpath>元素在目标的类路径上显式包含文件夹/文件 - 有关一些不太清晰的文档,请参阅Apache Ant手册

This would be: 这将是:

<classpath>
  <pathelement path="${path.to.jdk}"/>
</classpath>

See also this answer . 另见这个答案

NetBeans has setting which java to use internally. NetBeans设置了内部使用的java。 It's part of the netbeans.conf file. 它是netbeans.conf文件的一部分。 You are able to set the Java version for every NetBeans project which might be different. 您可以为每个可能不同的NetBeans项目设置Java版本。

However your JAVA_HOME points to a JRE and not JDK. 但是你的JAVA_HOME指向JRE而不是JDK。 That's why it cannot find the compiler - javac . 这就是为什么它找不到编译器--javac

The error is occurring when calling the Ant javac task. 调用Ant javac任务时发生错误。 Try to locate the call to that task (it should be in common.xml at the line shown in the error message), and try adding fork="yes" to it so that it runs the JDK compiler in an external process. 尝试找到对该任务的调用(它应该在错误消息中显示的行的common.xml中),并尝试向它添加fork="yes" ,以便它在外部进程中运行JDK编译器。 See http://ant.apache.org/manual/Tasks/javac.html : http://ant.apache.org/manual/Tasks/javac.html

<javac fork="yes" ...

Also try adding the following property in the project invocation: 还尝试在项目调用中添加以下属性:

Project p = new Project();
p.setUserProperty("ant.file", fileBuild.toFile().getAbsolutePath());
p.setProperty("java.home", "C:\\Program Files (x86)\\Java\\jdk1.7.0_45");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM