Results 1 to 15 of 15
  1. #1
    henry123 is offline Member
    Join Date
    Feb 2011
    Posts
    22
    Rep Power
    0

    Default buil a JAR file problem

    Hui I have a JAVA application, MyApplicationA which has some Jars and use them. Now I will to pack this application, MyApplicationA to a MyApplicationA.Jar and add it to classpath of another application. MyApplicationB which will call MyApplication.MyPackage.MyClass. The JAR command creates the JAR but now when I try to access MyApplicationA.MyClass from MyApplicationB.Client.java I get

    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
    at com.appA.dif.packageA.MyApplicationA.<clinit>(MyCl assA.java:18)
    at com.appB.other.package.MyClient.main(MyClient.java :18)
    Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.j ava:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 07)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:2 48)
    ... 2 more
    PS: the log4j-Jar exist already in MyapplicationA\lib and is exported to the JAR file too!!!
    Thanks for any Help

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default

    I hope you didn't put that other jar file in your jar file because that doesn't work. Your jar file should have a manifest entry that contains the classpath with the other jar files (and/or class files) your jar file depends on.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    henry123 is offline Member
    Join Date
    Feb 2011
    Posts
    22
    Rep Power
    0

    Default

    Default
    Hi
    I see that I did some thing very wrong!
    Now I changed the configuration build a simple Hello World Projekt as Jar.
    Itry to call this Jar from other client code, nevertheless I get the same error that
    NoClassFound ......:
    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger

    I put the log4j.jar in Manifest even as absolute, but I get the same Error when I try to call MySimpleApp.jar in the MayClient.java !!!!
    ??

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    What does your structure look like?
    How does the location of the log4j.jar compare with the declaration in the Manifest?

    For example, a simple app with log4j.jar as a dependency will have a structure like:
    Java Code:
    MyApp ->
            myjar.jar
            lib ->
                    log4j.jar
    So my app folder contains my jar, and a lib folder which contains the log4j.jar.
    The manifest will reference the log4j.jar as:
    Java Code:
    Class-Path: lib/log4j.jar

  5. #5
    henry123 is offline Member
    Join Date
    Feb 2011
    Posts
    22
    Rep Power
    0

    Default

    this is my Manifest:
    Manifest-Version: 1.0
    Class-Path: activation-1.1.jar ant-1.6.1.jar
    bcprov-jdk15-144.jar binding-2.0.1.jar
    jxbrowser-2.5.jar log4j-1.2.14.jar


    The JAR Command exports evry thing properly. That means in created MyJar.jar under lib \ there are all my JARs as expected:
    lib\log4j-1.2.14.jar
    lib\activation-1.1.jar
    lib\otherjar2.jar
    .....
    .
    It seems that the JVM does not find these JAR files on runtime?!
    I think the error "Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger"
    is confusing because if I had not a Logger in my Classes which is the first Object in any class that calls a class from a JAR file, the error message could referencing any other class that client code calls as first ?!

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    You cannot jar a jar file.
    Your dependent jars have to be supplied as separate files.

    Your Manifest is telling the JVM that those jars are in the same directory as the jar that contains that Manifest.

  7. #7
    henry123 is offline Member
    Join Date
    Feb 2011
    Posts
    22
    Rep Power
    0

    Default

    I can not understand you, sorry! does it mean. in my created JAR, there could not be other JAR files ?? If so , where should I put them, and how tell to the created JAR file , wehere they are ?
    When you say "Your dependent jars have to be supplied as separate files." that means I should unzip them ?
    Please give a sample, thanks

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default

    You classpath isn't correct: those jar files are stored in a lib sub directory relative to your 'main' jar file, so:

    Java Code:
    this is my Manifest:
      lib/bcprov-jdk15-144.jar 	lib/binding-2.0.1.jar
      lib/jxbrowser-2.5.jar 	lib/log4j-1.2.14.jar
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    From what I can tell from your posts, you have put the jar files into MyApp.jar, along with your code.
    This does not work.
    The JVM cannot reference jar files inside other jar files.

    You have to provide MyApp.jar, as well as all the other dependent jars.

  10. #10
    henry123 is offline Member
    Join Date
    Feb 2011
    Posts
    22
    Rep Power
    0

    Default

    I thougth with a relative path the "main.jar" find them correctly!Now I changed the manifest and removed the subdirectory lib and still the JAR command put them in a lib sub directory!!! how can I tell the Jar command to do not add them to a lib sub directory but to the root directory? Thank you for giving a clear sample in advance!

  11. #11
    henry123 is offline Member
    Join Date
    Feb 2011
    Posts
    22
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    From what I can tell from your posts, you have put the jar files into MyApp.jar, along with your code.
    This does not work.
    The JVM cannot reference jar files inside other jar files.

    You have to provide MyApp.jar, as well as all the other dependent jars.
    Do you means, all referened Jar files and MyApp.jar slhould be in the same hirarchy, sample:
    C:\x\y\server\myDirectory\MyApp.jar
    C:\x\y\server\myDirectory\log4j.jar
    C:\x\y\server\myDirectory\other1.jar
    C:\x\y\server\myDirectory\other2.jar
    ...
    ?

  12. #12
    henry123 is offline Member
    Join Date
    Feb 2011
    Posts
    22
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    You classpath isn't correct: those jar files are stored in a lib sub directory relative to your 'main' jar file, so:

    Java Code:
    this is my Manifest:
      lib/bcprov-jdk15-144.jar 	lib/binding-2.0.1.jar
      lib/jxbrowser-2.5.jar 	lib/log4j-1.2.14.jar
    kind regards,

    Jos
    It was mistake in my post but the relative path was exactly like your:
    my Manifest:
    lib/bcprov-jdk15-144.jar
    ......

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Quote Originally Posted by henry123 View Post
    Do you means, all referened Jar files and MyApp.jar slhould be in the same hirarchy, sample:
    C:\x\y\server\myDirectory\MyApp.jar
    C:\x\y\server\myDirectory\log4j.jar
    C:\x\y\server\myDirectory\other1.jar
    C:\x\y\server\myDirectory\other2.jar
    ...
    ?
    Well, according to the Manifest you have shown us it should be:
    C:\x\y\server\myDirectory\MyApp.jar
    C:\x\y\server\myDirectory\lib\log4j.jar
    C:\x\y\server\myDirectory\lib\other1.jar
    C:\x\y\server\myDirectory\lib\other2.jar

  14. #14
    henry123 is offline Member
    Join Date
    Feb 2011
    Posts
    22
    Rep Power
    0

    Default

    I think I was not clear enough and it confused you! Actually it is a Java application which has some Jars in its sub directory, lib and as usual these JARs are referenced in classpath of the application. What I tried, was to build a Jar from this application,add it to the another application classpath and call the functionality of the first application by the second application. Actually a usual case! I did not try to refer a jar inside another JAR from outside!! The problem is now solved. The reason was the missing JARs in the classpath of the Client!
    Thanks for your help

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Well, if you're not creating an executable jar then there's no need to supply a Class-Path entry in the manifest. It's only used on an executable jar.

Similar Threads

  1. Got problem with jar-file, any help?
    By Igbear in forum Advanced Java
    Replies: 7
    Last Post: 11-20-2010, 09:10 PM
  2. File problem
    By Blackberrylerner in forum New To Java
    Replies: 29
    Last Post: 08-20-2010, 02:00 PM
  3. .BAT File problem
    By egbert95 in forum New To Java
    Replies: 8
    Last Post: 08-03-2010, 01:26 AM
  4. problem with jar file
    By biba84 in forum Advanced Java
    Replies: 7
    Last Post: 11-09-2008, 06:46 PM
  5. problem with jar file pls help
    By jinu5 in forum New To Java
    Replies: 0
    Last Post: 08-15-2007, 10:41 PM

Posting Permissions

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