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 4 of 4

Thread: Running jars from commandline

  1. #1
    Junior Member
    Join Date
    Jul 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Running jars from commandline

    Hi,
    I have a testing application which runs perfectly on the eclipse IDE. It was developed by an excolleagiue, so there is not much documentation.
    Now it is said, that the same application could be executed in command line as well.

    So, I export the jar, point the Main class and export it to the same directory as the project.


    Eg:

    Project

    ------------------SimpleJar.jar(inside directory)

    ------------------src(inside directory)

    ------------------bin(inside directory)

    |-------------->package1 (inside bin)

    |-------------->package2 (inside bin)

    |------------->Main.class (inside package2)







    The structure is as shown above.

    My Main.class is programmed to take arguments.



    So now, from the command line, I go to the directory.. I say java-jar SimpleJar.jar info, I expect to give a 2-3 line intodcution.

    However i get a response like



    Error: Unable to initialize main class testclient.Main

    Caused by: java.lang.NoClassDefFoundError: org/junit/runners/model/InitializationError



    Another thing I did is, i moved to the bin folder and directly did the java Main.class info. Yet same error.



    Please let me know

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,401
    Thanks
    54
    Thanked 2,637 Times in 2,587 Posts

    Default Re: Running jars from commandline

    java.lang.NoClassDefFoundError: org/junit/runners/model/InitializationError
    Where is that class file located? Is it in a jar file? How can the JVM find it?

    One way to add a jar file to the classpath so the JVM can find it is to add a line to the main jar file's manifest file:
    For example, here is the contents of the .mnf file used to build a jar file:
    Main-Class: GetHandDetailsFromDDSsqlFile
    Class-path: sqlite-jdbc-3.23.1.jar
    The line in red points to another jar file the program needs to execute.

    Another way is to used the java command's -cp option to point to all the jar files needed for the program. This is not used with the -jar option.
    java -cp jar1.jar;jar2.jar TheMainClass
    TheMainClass is in one of the jar files pointed to the the -cp option
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Running jars from commandline

    Hi, Thank you for the reply.

    The main class is already in the jar file.
    The contents of my manifest file are as below:
    --------------------
    Manifest-Version: 1.0
    Main-Class: testclient.Main
    ---------------------

    as per the last suggestion in your reply.. I run the command this way after researching a bit
    --------------------------
    java -cp MyJar.jar;lib/*; /bin/testclient.Main
    ----------------------

    I believe my command is wrong here. Not understanding the mistake though

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,401
    Thanks
    54
    Thanked 2,637 Times in 2,587 Posts

    Default Re: Running jars from commandline

    java -cp MyJar.jar;lib/*; /bin/testclient.Main
    use a . as package separators not /
    For example the full class name for the Main class would be bin.testclient.Main
    However the manifest file says the full class name is testclient.Main (See Main-Class: entry in manifest file). I do not know why there is a /bin/ at the start of the Main class's name.

    What messages does the java program display on the console?

    The -cp needs to include a reference to the jar file that contains: org/junit/runners/model/InitializationError.class
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. add jars to project in intellij
    By hossein.k in forum Java IDEs
    Replies: 1
    Last Post: April 6th, 2013, 02:41 PM
  2. Question about External Jars
    By Azurit3 in forum Java Theory & Questions
    Replies: 6
    Last Post: August 27th, 2012, 01:41 PM
  3. OS Specific Jars
    By sinistaDev80 in forum Java Theory & Questions
    Replies: 0
    Last Post: March 26th, 2011, 09:29 AM
  4. Applet Deployment / Netbeans / Commandline
    By crystalbass17 in forum Java Applets
    Replies: 0
    Last Post: February 18th, 2011, 09:23 PM
  5. Programmatically Creating Jars
    By aussiemcgr in forum Java Theory & Questions
    Replies: 8
    Last Post: October 6th, 2010, 11:51 AM

Tags for this Thread