英文:
Jar Files not working when I include external dependencies in my project in Intellij
问题
I am using Java JDK Version 11 and Intellij IDEA
Case 1:
我创建了一个简单的Maven项目,创建了一个名为Main的类,然后在其中创建了一个main函数并编写了一些代码。
package org.example;
public class Main {
public static void main(String[] args) {
System.out.printf("Hello and welcome!");
for (int i = 1; i <= 5; i++) {
System.out.println("i = " + i);
}
}
}
在这种情况下,我的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>org.example</groupId>
<artifactId>MyProject</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
然后我创建了这个Main.java文件的一个jar包,并尝试使用"java -jar CreatedJarname.jar"运行它。它成功运行并输出结果
Case 2:
现在我想要使用Spark。因此,我使用UI在我的项目中包含了一些外部jar包。
在附加的图片中,您可以看到名为"activation-1.1.1 jar and 242 more files"的添加依赖项。
现在,我能够在我的项目中使用Spark。但是我的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>org.example</groupId>
<artifactId>MyProject</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
而且,现在当我在main方法中使用与上面相同的代码创建一个jar包,并使用"java -jar CreatedJarname.jar"命令运行它时,我收到以下错误 -
错误:找不到或加载主类org.example.Main
原因:java.lang.ClassNotFoundException: org.example.Main
因此,现在我非常困惑,仅仅添加外部依赖项到我的项目会导致我的jar文件出现错误。此外,当添加这些依赖项时,我的pom.xml文件没有得到更新。我已经尝试在网络上寻找解决方案,但没有找到任何解决方法。
英文:
I am using Java JDK Version 11 and Intellij IDEA
Case 1:
I created a simple maven project, created a class named Main and then a main function inside it and wrote some code.
package org.example;
public class Main {
public static void main(String[] args) {
System.out.printf("Hello and welcome!");
for (int i = 1; i <= 5; i++) {
System.out.println("i = " + i);
}
}
}
My POM.XML file in this case looks like -
<?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>org.example</groupId>
<artifactId>MyProject</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
Then I created a jar of this Main.java file and tried to run using "java -jar CreatedJarname.jar". It runs and gives the output successfully
Case 2:
Now I wanted to work with spark. So, I included some external jars in my project using this UI.
In the attached pic, you can see the added dependencies named "activation-1.1.1 jar and 242 more files".
Now, I am able to use spark in my project. However my pom.xml has remained unchanged. It didn't get updated.
<?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>org.example</groupId>
<artifactId>MyProject</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
Also, now when I create a jar using the same code in the main method as above, and use the command "java -jar CreatedJarname.jar" to run it, I am getting the error -
Error: Could not find or load main class org.example.Main
Caused by: java.lang.ClassNotFoundException: org.example.Main
So, now I am very confused, just adding external dependencies to my project causes my jar files to have an error. Also, my pom.xml is not getting updated when adding these dependencies. I have tried looking over the web but I am not able to find any solution.
答案1
得分: 2
-
如果这是一个Maven项目,您需要在
pom.xml
中添加所有依赖项。 -
要使一个JAR文件可执行,您需要使用Maven Assembly插件或Maven Shade插件构建它。普通的JAR文件不包含它们的依赖项。
英文:
Two misunderstandings:
-
If this is a Maven project, you need to add all dependencies in the
pom.xml
. -
To make a JAR executable, you need to build it with the Maven Assembly Plugin or the Maven Shade Plugin. Normal JARs do not include their dependencies.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论