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: Class Not found in jar

  1. #1
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    284
    My Mood
    Amused
    Thanks
    6
    Thanked 63 Times in 61 Posts

    Default Class Not found in jar

    Hi everyone.
    I have a jar file.
    and i imported its classes to my program..
    let say i have a code
    source code is at /home/t_bmf/Java/src
    import firstjar.FirstJarPrint;
     
    public class TestJar {
            public static void main(String[] args) {
                    //FirstJarPrint jar = new FirstJarPrint();
            }
    }
    well i have successfully compiled it using command below:
    javac -cp ".:/home/t_bmf/Java/lib/FirstJar.jar" -d /home/t_bmf/Java/bin TestJar.java

    it means that i don't have any compilation error right?
    so all classes found properly.

    But whenever I run the program I always got this error:
    java -cp /home/t_bmf/Java/lib/FirstJar/jar:. TestJar
    Exception in thread "main" java.lang.NoClassDefFoundError: firstjar/FirstJarPrint
    at TestJar.main(TestJar.java:6)
    Caused by: java.lang.ClassNotFoundException: firstjar.FirstJarPrint
    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 06)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:2 47)
    ... 1 more
    I think it has to be no error since i have compiled the program successfully right?
    if it really didn't find the class, then it must be a compilation error right?

    is there something wrong with the way I execute the program??
    Thank you!

    note: i created the program in UNIX


  2. #2
    Member
    Join Date
    Feb 2014
    Location
    India
    Posts
    47
    My Mood
    Bored
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Default Re: Class Not found in jar

    Is there a mistake in the classpath. i.e.

    should it be
    javac -cp ".:/home/t_bmf/Java/lib/firstjar/FirstJar.jar" -d /home/t_bmf/Java/bin TestJar.java
    instead of
    javac -cp ".:/home/t_bmf/Java/lib/FirstJar.jar" -d /home/t_bmf/Java/bin TestJar.java

  3. #3
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    284
    My Mood
    Amused
    Thanks
    6
    Thanked 63 Times in 61 Posts

    Default Re: Class Not found in jar

    nope
    i have a directory
    /home/t_bmf/Java/lib :which must contains jar files.
    /home/t_bmf/Java/src :which must contains java files.
    /home/t_bmf/Java/bin :which must contains .class files.


    i have created a jar file FirstJar.jar for testing only.
    inside of that jar file is a package firstjar and in that package is where the FirstJarPrint.class is located
    I think there was no mistaken in classpath.

    I have tried this one:
    I paste FirstJar.jar file to /home/t_bmf/Java/bin (which must contain only class file)
    and i tried this command (in bin directory):
    java -cp $CLASSPATH:.:FirstJar.jar TestJar
    well, it works
    I also remove the comment that makes me create an object of instance of FirstJarPrint and used all its method.
    And it worked perfectly.

    But i don't want this (having /home/t_bmf/Java/bin directory contain a jar file)
    therefore i remove the jar file in /home/t_bmf/Java/bin.
    and i tried this command:
    java -cp $CLASSPATH:.:home/Java/lib/FirstJar.jar TestJar
    but the same exception occurs again.
    Exception in thread "main" java.lang.NoClassDefFoundError: firstjar/FirstJarPrint
    at TestJar.main(TestJar.java:6)
    Caused by: java.lang.ClassNotFoundException: firstjar.FirstJarPrint
    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 06)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:2 47)
    ... 1 more

    is there something wrong with my command? thanks a lot

    --- Update ---

    i made it now.
    it should be:
    java -cp $CLASSPATH:.:/home/Java/lib/FirstJar.jar TestJar
    instead of:
    java -cp $CLASSPATH:.:home/Java/lib/FirstJar.jar TestJar
    thank your ankurt.
    you helped me again..

  4. #4
    Member
    Join Date
    Feb 2014
    Location
    India
    Posts
    47
    My Mood
    Bored
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Default Re: Class Not found in jar

    I am Happy to help you.

  5. The Following User Says Thank You to ankurt For This Useful Post:

    dicdic (February 14th, 2014)

Similar Threads

  1. Writing a class - class not found
    By nepperso in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 23rd, 2013, 12:51 AM
  2. [SOLVED] jar file not found
    By user2013 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 13th, 2013, 02:12 PM
  3. class not found
    By aki in forum Java Applets
    Replies: 1
    Last Post: May 23rd, 2012, 06:50 AM
  4. What is wrong with this code class not found
    By newbieJava in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 19th, 2011, 05:39 PM
  5. Replies: 2
    Last Post: January 8th, 2010, 07:22 AM