简体   繁体   English

Netbeans java maven 项目成功构建,但无法运行

[英]Netbeans java maven project builds sucesfully, but won't run

When I clean & build, the project says "build success" and I can see the .jar file created in the target folder, but when I try to run or debug the main class netbeans says "Error: Could not find or load main class..."?当我清理和构建时,项目显示“构建成功”,我可以看到在目标文件夹中创建的 .jar 文件,但是当我尝试运行或调试主类时,netbeans 显示“错误:无法找到或加载主类……”? I can run the .jar file from the command line.我可以从命令行运行 .jar 文件。

Edit: this is a javaFx project.编辑:这是一个 javaFx 项目。

It may be due to some reasons, you can try these:可能是由于某些原因,您可以尝试以下方法:

  1. Main Class is not declared as "PUBLIC".主类未声明为“PUBLIC”。
  2. Java Environment path should be set correctly .正确设置Java 环境路径。
  3. Try to change the location of project.尝试更改项目的位置。

I've partially solved the problem.我已经部分解决了这个问题。 It appears that netbeans has some issues with javaFX projects.看来netbeans 对javaFX 项目有一些问题。 I found several sites indicating problems.我发现了几个表明问题的网站。

I was able to get it working by creating a new Maven / JavaFX application, then I took the nbactions.xml file from that project and replaced the nbactions.xml in my project.我能够通过创建一个新的 Maven/JavaFX 应用程序来使其工作,然后我从该项目中获取 nbactions.xml 文件并替换了我项目中的 nbactions.xml。 After that, I can run the app by selecting the main project node and using the run/debug buttons on the toolbar.之后,我可以通过选择主项目节点并使用工具栏上的运行/调试按钮来运行应用程序。 Right clicking the Main class and selecting run or debug still does not work.右键单击 Main 类并选择运行或调试仍然不起作用。 Thanks to @skomisa for reminding me there is more than one way to run a project!感谢@skomisa 提醒我有不止一种方法可以运行一个项目!

Here's the nbactions.xml file that I copied that works:这是我复制的有效的 nbactions.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<actions>
  <action>
    <actionName>run</actionName>
    <goals>
      <goal>clean</goal>
      <goal>package</goal>
      <goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
    </goals>
    <properties>
      <runfx.args>-jar "${project.build.directory}/${project.build.finalName}.jar"</runfx.args>
    </properties>
  </action>
  <action>
    <actionName>debug</actionName>
    <goals>
      <goal>clean</goal>
      <goal>package</goal>
      <goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
    </goals>
    <properties>
      <runfx.args>-Xdebug -Xrunjdwp:transport=dt_socket,server=n,address=${jpda.address} -Dglass.disableGrab=true -jar "${project.build.directory}/${project.build.finalName}.jar"</runfx.args>
      <jpda.listen>true</jpda.listen>
    </properties>
  </action>        
</actions>

Here's an example of previous the nbactions.xml file that did not work.这是以前无效的 nbactions.xml 文件的示例。 Notice the properties for the run and debug actions are quite a bit different.请注意,运行和调试操作的属性有很大不同。

<?xml version="1.0" encoding="UTF-8"?>
<actions>
  <action>
    <actionName>run</actionName>
    <packagings>
      <packaging>jar</packaging>
    </packagings>
    <goals>
      <goal>process-classes</goal>
      <goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
    </goals>
    <properties>
      <exec.args>-classpath %classpath com.edulog.athenaprobe.MainApp</exec.args>
      <exec.executable>java</exec.executable>
    </properties>
  </action>
  <action>
    <actionName>debug</actionName>
    <packagings>
      <packaging>jar</packaging>
    </packagings>
    <goals>
      <goal>process-classes</goal>
      <goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
    </goals>
    <properties>
      <exec.args>-Xdebug -Xrunjdwp:transport=dt_socket,server=n,address=${jpda.address} -classpath %classpath com.edulog.athenaprobe.MainApp</exec.args>
      <exec.executable>java</exec.executable>
      <jpda.listen>true</jpda.listen>
    </properties>
  </action>

</actions>

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

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