Results 1 to 6 of 6
  1. #1
    rovf is offline Member
    Join Date
    Aug 2013
    Posts
    10
    Rep Power
    0

    Question Can not specify additional JAR file(s) when main is in a Jar file

    I am on Windows, using Java 1.7, and have a file

    jsrc\Jmain.java

    which contains the Main program, and also imports several classes which are stored in the file

    c:\jruby-9.0.4.0\lib\jruby.jar

    I compile this with

    set CLASSPATH=c:\jruby-9.0.4.0\lib\jruby.jar;jsrc
    javac jsrc\Jmain.java

    I can execute this file by doing this:

    cd jsrc
    java -cp c:\jruby-9.0.4.0\lib\jruby.jar;. Jmain

    Now I would like to change this setup, by placing Jmain.class into a jar file and execute it from there. Therefore, I create a file MANIFEST.MF in the base directory with the following contents:

    Manifest-Version: 1.0
    Main-Class: Jmain

    After compiling (as above), I create the jar file with:

    jar cvfm jars\vp.jar MANIFEST.MF -C jsrc Jmain.class

    When I run the jar file using

    java -cp c:\jruby-9.0.4.0\lib\jruby.jar -jar jars\vp.jar

    my Jmain is found, but the definitions in jruby.jar are not found. For example, my Jmain contains the import statement

    import org.jruby.embed.ScriptingContainer;

    and uses it as

    ScriptingContainer container = new ScriptingContainer(LocalContextScope.SINGLETHREAD) ;

    but when running it, this assignment causes the error

    Exception in thread "main" java.lang.NoClassDefFoundError: org/jruby/embed/ScriptingContainer
    at Jmain.main(Jmain.java:9)
    Caused by: java.lang.ClassNotFoundException: org.jruby.embed.ScriptingContainer
    at java.net.URLClassLoader$1.run(Unknown Source)

    What did I do wrong?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Can not specify additional JAR file(s) when main is in a Jar file

    java -cp c:\jruby-9.0.4.0\lib\jruby.jar;. Jmain
    Use the same commandline structure as above and add all the needed jar files to the -cp option. Don't use the -jar option
    Or add a Class-Path line to the manifest file that references the other jar files needed.

    See the tutorial: Lesson: Packaging Programs in JAR Files (The Java™ Tutorials > Deployment)
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    rovf is offline Member
    Join Date
    Aug 2013
    Posts
    10
    Rep Power
    0

    Default Re: Can not specify additional JAR file(s) when main is in a Jar file

    Your second suggestion (adding the other jar files to the manifest using "Class-Path:") did not work (Java then complains that it does not find the class Jmain - I have the impression that it searches for Jmain then only inside the jruby.jar). However, your first suggestion, adding both jar files to -cp and dropping the -jar option, works:

    java -cp jars\vp.jar;c:\jruby-9.0.4.0\lib\jruby.jar Jmain

    Thanks a lot!

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Can not specify additional JAR file(s) when main is in a Jar file

    Quote Originally Posted by rovf View Post
    Your second suggestion (adding the other jar files to the manifest using "Class-Path:") did not work (Java then complains that it does not find the class Jmain - I have the impression that it searches for Jmain then only inside the jruby.jar).
    It should have worked.
    The Manifest should have the Class-Path:
    Java Code:
    Class-Path: c:\jruby-9.0.4.0\lib\jruby.jar
    (note you would normally have a lib directory in the same folder as your executable jar, and stick your dependencies in there).

    And the command to run it would be (if you were in the same directory as the jar file):
    java -jar vp.jar
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    rovf is offline Member
    Join Date
    Aug 2013
    Posts
    10
    Rep Power
    0

    Default Re: Can not specify additional JAR file(s) when main is in a Jar file

    Quote Originally Posted by Tolls View Post
    It should have worked.
    The Manifest should have the Class-Path:
    Java Code:
    Class-Path: c:\jruby-9.0.4.0\lib\jruby.jar
    (note you would normally have a lib directory in the same folder as your executable jar, and stick your dependencies in there).

    And the command to run it would be (if you were in the same directory as the jar file):
    java -jar vp.jar
    Yes, this was exactly how I did it, but then Jmain wasn't found. It's weird.

    BTW, I also made sure that the Manifest file ends in a newline, because this can cause problems otherwise. I also tried to replace backslashes by forward slashes, just in case.

    Anyway, while I would like to solve it using the manifest file just for curiosity too, at least the other suggestion you made worked for me.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Can not specify additional JAR file(s) when main is in a Jar file

    I wonder if it's because it isn't in a package.
    There are curious side-effects of not having a class in a package, and this could be one of them.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. File IO reader outside of main?
    By nhmllr in forum New To Java
    Replies: 5
    Last Post: 01-10-2012, 07:07 AM
  2. printing in external file outside of main
    By droidus in forum New To Java
    Replies: 0
    Last Post: 04-01-2011, 10:16 PM
  3. .jar file cant find main class
    By eLancaster in forum New To Java
    Replies: 3
    Last Post: 02-10-2011, 11:17 AM
  4. Jar file could not find the main class
    By Collinryans in forum New To Java
    Replies: 3
    Last Post: 06-23-2010, 04:18 AM
  5. making a .main file
    By h3ckf1r3 in forum NetBeans
    Replies: 4
    Last Post: 06-30-2008, 07:42 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •