Jar Files not working when I include external dependencies in my project in Intellij

huangapple go评论67阅读模式
英文:

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包。

Jar Files not working when I include external dependencies in my project in Intellij

在附加的图片中,您可以看到名为"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(&quot;Hello and welcome!&quot;);
        for (int i = 1; i &lt;= 5; i++) {

            
            System.out.println(&quot;i = &quot; + i);
        }
    }
}

My POM.XML file in this case looks like -

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
         xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

    &lt;groupId&gt;org.example&lt;/groupId&gt;
    &lt;artifactId&gt;MyProject&lt;/artifactId&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;

    &lt;properties&gt;
        &lt;maven.compiler.source&gt;11&lt;/maven.compiler.source&gt;
        &lt;maven.compiler.target&gt;11&lt;/maven.compiler.target&gt;
        &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
    &lt;/properties&gt;

&lt;/project&gt;

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.

Jar Files not working when I include external dependencies in my project in Intellij

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.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
         xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

    &lt;groupId&gt;org.example&lt;/groupId&gt;
    &lt;artifactId&gt;MyProject&lt;/artifactId&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;

    &lt;properties&gt;
        &lt;maven.compiler.source&gt;11&lt;/maven.compiler.source&gt;
        &lt;maven.compiler.target&gt;11&lt;/maven.compiler.target&gt;
        &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
    &lt;/properties&gt;

&lt;/project&gt;

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

  1. 如果这是一个Maven项目,您需要在pom.xml中添加所有依赖项。

  2. 要使一个JAR文件可执行,您需要使用Maven Assembly插件或Maven Shade插件构建它。普通的JAR文件不包含它们的依赖项。

英文:

Two misunderstandings:

  1. If this is a Maven project, you need to add all dependencies in the pom.xml.

  2. 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.

huangapple
  • 本文由 发表于 2023年6月12日 00:46:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451516.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定