gpt4 book ai didi

Java项目启用Maven后无法运行

转载 作者:行者123 更新时间:2023-12-02 13:42:52 25 4
gpt4 key购买 nike

在启用 maven 之前,我可以在 Netbeans 上构建(运行/调试)我的 Java 项目,以更好地处理依赖性。现在项目已成功构建,但我无法运行/调试它。

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

当我双击创建的 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] ------------------------------------------------------------------------

最佳答案

如果您的 jar 无法执行,会发生什么情况,因为:

  • 指定主类的 list 条目不在这里,
  • 和/或所需的依赖项未与 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 compile assembly:single

将组装任务与打包阶段联系起来是很常见的,以确保在执行mvn package时构建具有依赖项的jar。

关于Java项目启用Maven后无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42634248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com