Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Run Java Class with External Dependencies from Command Prompt?

  1. #1
    Junior Member
    Join Date
    Mar 2024
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

  2. #2
    Member
    Join Date
    Jan 2024
    Posts
    75
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Post Re: Run Java Class with External Dependencies from Command Prompt?

    To run a Java class with external dependencies from the Windows Command Prompt, you need to include the required external libraries (in this case, the Jackson library) in your classpath. Here's how you can do it:

    Navigate to the directory containing your compiled Java class file (Test.class) using the Command Prompt.

    Use the java command to run your Java class, specifying the classpath using the -cp or -classpath option to include the external dependencies. You'll need to include the paths to both your compiled class and the external JAR files. Assuming your Jackson library JAR file is named jackson-core.jar and is located in a lib directory relative to your class file, the command would look like this:

    java -cp .;lib/jackson-core.jar Test

    Here, . represents the current directory, and lib/jackson-core.jar specifies the path to the Jackson library JAR file relative to the current directory.

    Hit Enter to execute the command. If everything is set up correctly, your Java class should run, utilizing the external dependencies specified in the classpath.

    Remember to adjust the paths and filenames in the command according to your project's structure and the actual names of your external dependencies.

    When navigating the complexities of Java assignments, it's beneficial to seek additional support and resources. Whether you're grappling with intricate concepts or seeking guidance to elevate your skills, platforms like programminghomeworkhelp.com can offer valuable help with Java assignment. Through expert guidance and tailored support, you can enhance your understanding and proficiency in Java programming. Remember, seeking help is a proactive step towards mastering Java and achieving your academic or professional goals.

Similar Threads

  1. CommaND Prompt:class, interface, or enum expected
    By Chinedu & ICT in forum What's Wrong With My Code?
    Replies: 11
    Last Post: December 29th, 2019, 04:47 PM
  2. why the command prompt does not recognize java as a command?
    By bihlas in forum Java Theory & Questions
    Replies: 1
    Last Post: July 22nd, 2014, 09:50 AM
  3. Java command prompt
    By Tanmaysinha in forum Java Theory & Questions
    Replies: 2
    Last Post: August 30th, 2011, 09:30 AM
  4. Java command prompt
    By Tanmaysinha in forum Java Theory & Questions
    Replies: 1
    Last Post: August 29th, 2011, 03:05 PM
  5. Java on command prompt
    By Tanmaysinha in forum What's Wrong With My Code?
    Replies: 12
    Last Post: August 17th, 2011, 06:45 AM

Tags for this Thread