Results 1 to 7 of 7
  1. #1
    orestis125 is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default Java build error: NoClassDefFoundError

    Hello everyone,

    First of all, I'm using NetBeans to program in Java. I've finished my application and I've compiled my files into 1 jar file. The problem is that I'm getting an error when I double click the .jar file. The error is: "Exception in thread "main" java.lang.NoClassDefFoundError: org/jnetpcap/Pcap"

    I've searched online for solutions but I couldn't find anything that would solve my problem. Basically this error occurs when a class cannot be found now but could be found in run-time. My Manifest.MF file's contents is:

    Manifest-Version: 1.0
    Ant-Version: Apache Ant 1.8.3
    Created-By: 1.7.0_04-b22 (Oracle Corporation)
    Class-Path:
    X-COMMENT: Main-Class will be added automatically by build
    Main-Class: package.Main
    I'm assuming that the error lies on the Class-Path value. However, I have no idea what to set the value to. My project uses external libraries, so I tried using the path of those libraries as the class-path (C:\Program Files\Java\jdk1.7.0_04\jre\lib). When I do that, I get an error "Could not find or load main class". At that point, I assumed that inside the class-path directory my project's classes must also exist. Then, I copied the whole lib folder and pasted it inside the .jar file and also changed the class-path to "/lib". This resulted in the first error. I've tried many combinations of directories and class-path values but they all result in the first error.

    I would appreciate it if someone could help me out. Keep in mind that, if possible, I need my external libraries inside the jar file (or in the same directory as the jar file) since I want to be able to distribute the project.

    Thank you very much
    Last edited by orestis125; 10-04-2012 at 10:46 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Java build error: NoClassDefFoundError

    Keep in mind that, if possible, I need my external libraries inside the jar file (or in the same directory of the jar file) since I want to be able to distribute the project.
    Do all applications that are distributed as .jar archives include third party libraries within a single jar?

    I'm pretty sure NetBeans facilitates this jar-within-jar idiom, but I'm not sure I like it. What happens if the 3rd party discovers some heinous security flaw in their code or just plain improves the implementation? Your users are stuck with the old (slow, buggy or flawed) version unless they rebuild your jar which they may be unable or unwilling to do.

    You can build the jar yourself at the command line. (Details in Oracle's Tutorial at the Packaging Programs in Jar Files chapter) Or carefully set the settings in the NetBeans project properties and jar building wizard. Sorry, I'm not familiar enough with the latter to know where you might have slipped, but others here may be. Perhaps it would help to post the relevant settings (external libraries in the project and the things you set when you built the jar or "exported as jar" or whatever NB might call it.)

  3. #3
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,842
    Rep Power
    19

    Default Re: Java build error: NoClassDefFoundError

    Netbeans should be deploying any dependent jars into the dist directory where it creates your application jar.
    It should also (in normal usage) add the dependencies into the Manifest.

    I've not normally had to do anything.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    orestis125 is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default Re: Java build error: NoClassDefFoundError

    Thank you both for your replies,

    I don't mind if the dependent libraries are not inside the jar as long as they are in the same directory as the jar file. For some reason NetBeans doesn't copy the dependent libraries in the dist directory where it creates my application's jar file. I do have "Copy dependent libraries" ticked in my project's properties.

    If I find all the dependent libraries, and paste them in a lib folder in the same directory as the jar file, what should the class-path be? ("/lib" doesn't work).

    Thank you again for your help,
    I appreciate it

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,842
    Rep Power
    19

    Default Re: Java build error: NoClassDefFoundError

    The classpath would be:
    lib/*.jar

    (Actually lib/* might work)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    gowthamgutha is offline Member
    Join Date
    Sep 2012
    Location
    Guntur, India
    Posts
    27
    Rep Power
    0

    Default Re: Java build error: NoClassDefFoundError

    Check for the existence of the class, that it was showing 'Not Found'. The class file should be in the in the CLASSPATH. CLASSPATH is nothing but the combination of the class, the path where they lie usually in a jar file. Make sure you have set all the necessary files that your application needs, in the classpath. You can do it in NetBeans by adding external jars (the jars that were required for your application to run)

    Also check for the version of Java that NetBeans is using and the version installed in your PC. They should be one and the
    same.
    Last edited by DarrylBurke; 10-04-2012 at 03:48 PM. Reason: Removed font tag

  7. #7
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,842
    Rep Power
    19

    Default Re: Java build error: NoClassDefFoundError

    Quote Originally Posted by gowthamgutha View Post
    Also check for the version of Java that NetBeans is using and the version installed in your PC. They should be one and the
    same.
    Nope.
    The only requirement is that the major version used to run the compiled Java code is higher than the one used to compile it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Error: Could not find or load the main class build.jar
    By Stoyicker in forum New To Java
    Replies: 2
    Last Post: 05-24-2012, 09:58 PM
  2. Replies: 3
    Last Post: 10-17-2011, 10:10 AM
  3. when i build my project it give following error
    By enggvijaysingh@gmail.com in forum Advanced Java
    Replies: 0
    Last Post: 11-18-2010, 11:02 AM
  4. Error after build to jar file.
    By bach_meoden in forum AWT / Swing
    Replies: 13
    Last Post: 04-06-2010, 02:27 PM
  5. Error when build by j2me wireless toolkit
    By matvey in forum Sun Java Wireless Toolkit
    Replies: 3
    Last Post: 10-23-2009, 06:34 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
  •