启用Maven后,Java项目不会运行

时间:2017-03-06 19:45:25

标签: java maven debugging build

我可以在Netbeans上构建(运行/调试)我的Java项目,然后再使用 maven-enabled 来更好地处理依赖项。现在项目构建成功,但我无法运行/调试它。

当我单击调试/运行按钮时,在输出窗口中,我看到一条消息,表明项目已成功构建,但应用程序无法启动。

当我双击创建的jar文件时,我收到以下消息:无法启动Java JAR文件“myProject-SNAPSHOT.jar”。检查控制台是否有可能的错误消息。 但我没有在控制台上看到任何错误消息。

更新1 这就是我pom.xml的样子:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>MyGroupID</groupId>
    <artifactId>MyProjectName</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.15</version>
        </dependency>
        <dependency>
            <groupId>org.jfree</groupId>
            <artifactId>jcommon</artifactId>
            <version>1.0.23</version>
        </dependency>
    </dependencies>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
</project>

更新2 以下是mvn -X clean package的输出:

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building GeMSE 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ GeMSE ---
[INFO] Deleting /Users/user/NetBeansProjects/GeMSE_Maven_Enabled/GeMSE/target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ GeMSE ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/user/NetBeansProjects/GeMSE_Maven_Enabled/GeMSE/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ GeMSE ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 73 source files to /Users/user/NetBeansProjects/GeMSE_Maven_Enabled/GeMSE/target/classes
[WARNING] /Users/user/NetBeansProjects/GeMSE_Maven_Enabled/GeMSE/src/main/java/GeMSE/Importers/GTFParser.java: Some input files use unchecked or unsafe operations.
[WARNING] /Users/user/NetBeansProjects/GeMSE_Maven_Enabled/GeMSE/src/main/java/GeMSE/Importers/GTFParser.java: Recompile with -Xlint:unchecked for details.
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ GeMSE ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/user/NetBeansProjects/GeMSE_Maven_Enabled/GeMSE/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ GeMSE ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ GeMSE ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ GeMSE ---
[INFO] Building jar: /Users/user/NetBeansProjects/GeMSE_Maven_Enabled/GeMSE/target/GeMSE-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.838 s
[INFO] Finished at: 2017-03-06T15:10:25-08:00
[INFO] Final Memory: 19M/312M
[INFO] ------------------------------------------------------------------------

1 个答案:

答案 0 :(得分:1)

你的jar无法执行会发生什么,因为:

  • 指定主类的清单条目不在此处,
  • 和/或所需的依赖项未与JAR文件捆绑在一起。

您必须构建与依赖项捆绑在一起的jar。在pom.xml中定义:

<build>
<plugins>
  <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
      <archive>
        <manifest>
          <mainClass>fully.qualified.MainClass</mainClass>
        </manifest>
      </archive>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
    </configuration>
    <executions>
     <execution>
       <id>make-assembly</id> <!-- this is used for inheritance merges -->
       <phase>package</phase> <!-- bind to the packaging phase -->
       <goals>
        <goal>single</goal>
       </goals>
     </execution>
   </executions>
  </plugin>
</plugins>
</build>

然后您可以通过在终端中调用来创建JAR文件:

  

mvn clean编译程序集:单个

将程序集任务绑定到程序包阶段是很常见的,以确保在执行 mvn package 时构建具有依赖关系的jar。