Search code examples
javamavendependencies

Run Java Class with External Dependencies from Command Prompt?


I was wondering whether it is possible to run a Java class that uses external libraries using the Windows Command Prompt.

I have the following minimal example Java class, where I just use the Jackson library to create a JSON file using the ObjectMapper class:

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;

public class Test
{
    public static void main(String[] args) throws IOException
    {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.writeValue(new File("test.json"), "test");

        System.out.println("Finished!");
    }
}

I compiled this project originally using Maven in the IntelliJ IDE, and I set the proper dependencies for Jackson in the pom.xml file.

I then wanted to run the resultant Test.class file with the Command Prompt with the following command:

java Test

Resulting in:

Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ObjectMapper
        at Test.main(Test.java:9)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.ObjectMapper
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526)
        ... 1 more

I'm not very experienced with Java, and I've only ever used IDEs that handle most of the linking in the background for me. I just know that one can run a Java class in the Windows CMD using the java command, and I'm assuming that here it's telling me that it can't link the Jackson library properly.

Most of the existing simillar questions out there usually ask about user-made classes, I haven't yet been able to find a solution for external dependencies such as those defined by Maven.

Is it possible to run a Java class that uses external libraries using the Windows Command Prompt?

Thanks for reading my post, any guidance is appreciated.


Solution

  • After some time spent messing around with the Maven settings and some more searching, I think I found a solution to my issues.

    I had some misconceptions about how exactly Maven works, and I was confusing class and jar files in this case. I had initially assumed that I would have to strichtly run the generated Test.class file in order to run my program via the Command Prompt.

    However, as some have pointed out in the comments of my post (thanks Basil Bourque), Maven is capable of generating a jar file that contains everything required to run my program.


    Here are the steps that worked for my specific setup in IntelliJ IDE, adapted from the suggested post answer:

    1. Ensured that the correct dependencies are listed in pom.xml:
    <dependencies>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>2.16.0</version>
        </dependency>
    </dependencies>
    
    1. Added this section underneath the dependencies in pom.xml:
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>Test</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>
    
    1. Ran the clean Maven command under the Lifecycle category:

    enter image description here

    1. Ran the compile Maven command under the Lifecycle category:

    enter image description here

    1. Ran the assembly:single Maven command under the assembly plugin in the Plugins category:

    enter image description here


    After doing the steps above, in the target folder of my project there was now a jar file with the suffix jar-with-dependencies, which contains my program code alongside all the imported libraries.

    I was able to run it from the Command Prompt by issuing:

    java -jar *path_of_jar_file*
    

    Thanks for all the feedback!