The dreaded java.lang.NoClassDefFoundError

后端 未结 7 1542
隐瞒了意图╮
隐瞒了意图╮ 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: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
    

提交回复
热议问题