The dreaded java.lang.NoClassDefFoundError

后端 未结 7 1498
隐瞒了意图╮
隐瞒了意图╮ 2020-12-10 18:13

I\'ve looked through many of the existing threads about this error, but still no luck. I\'m not even trying to package a jar or use any third-party packaging tools. I\'m s

相关标签:
7条回答
  • 2020-12-10 18:42

    Consider File -> Export -> Runnable jar to create a jar file which can be invoked directly with

    java -jar yourProgram.jar
    

    There are several variants depending on your needs.

    0 讨论(0)
  • 2020-12-10 18:47

    The classpath setting you are setting in Eclispe are only for the IDE and do not affect how you application is run outside the IDE. Even if you use the Eclipse Functionality to export your application as an executable jar file there is no out of the box way to package all the jars your application depends on.

    If you have packaged you application into a jar file called myapp.jar then running a command like below will run the application with the jar you depend on, if you have more than one just add them separted by ; on Windows or : on Unix:

    java -jar myapp.jar -cp .;c:/pathtolibs/commons-lang-2.4.jar
    

    If you are just running the classes directly then either run the folder containing your .class files will also need to be on the path (though I assume it already is since you are able to run the program and get errors).

    0 讨论(0)
  • 2020-12-10 18:47

    Eclipse does not move any of the jars in your classpath into the bin folder of your project. You need to copy the util jar into the bin folder. If you move it to the root of the bin folder, you might be able to get away without any classpath entries but it's not the recommended solution. See @BalusC's answer for good coverage of that.

    0 讨论(0)
  • 2020-12-10 18:51

    make sure your jar commons-lang-2.4.jar in classpath and not redudance.

    I ever add jar file to my classpath, and have 2 file jar in my classpath. After I delete it, work smooth

    0 讨论(0)
  • 2020-12-10 18:55

    A NoClassDefFoundError basically means that the class was there in the classpath during compiletime, but it is missing in the classpath during runtime.

    In your case, when executing using java.exe from commandline, you need to specify the classpath in the -cp or -classpath argument. Or if it is a JAR file, then you need to specify it in the class-path entry of its MANIFEST.MF file.

    The value of the argument/entry can be either absolute or relative file system paths to a folder containing all .class files or to an individual .jar file. You can separate paths using a semicolon ;. When a path contains spaces, you need to wrap the particular path with doublequotes ". Example:

    java -cp .;c:/path/to/file.jar;"c:/spacy path/to/classes" mypackage.MyClass
    

    To save the effort of typing and editing the argument in commandline everytime, use a .bat file.

    Edit: I should have realized that you're using an Unix based operating system. The above examples are Windows-targeted. In the case of Unix like platforms you can follow the same rules, but you need to separate the paths using a colon : and instead of an eventual batch file, use a .sh file.

    java -cp .:/path/to/file.jar:"/spacy path/to/classes" mypackage.MyClass
    
    0 讨论(0)
  • 2020-12-10 18:55

    Are you specifying the classpath to java on the command line?

    $ java -cp lib/commons-lang-2.4.jar your.main.Class
    
    0 讨论(0)
提交回复
热议问题