Search code examples
javamavenapache-sparkintellij-ideajar

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:

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.

Adding external dependencies

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.


Solution

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