java.lang.NoClassDefFoundError when i run java file from terminal

后端 未结 5 1624
轻奢々
轻奢々 2020-12-20 06:44

I am a java newbie. I have been using Eclipse to test a simple java class (named NewHelloWorld) and it runs fine in the console. When I try to do the same thing from a termi

相关标签:
5条回答
  • 2020-12-20 07:16

    cd up to the root package. Most of the cases it will be src folder in the Project if created from eclipse IDE.

    java -cp . org.kodeplay.kodejava.NewHelloWorld should work

    java org.kodeplay.kodejava.NewHelloWorld should also work. I tried both the things and it works fine in both the case

    0 讨论(0)
  • 2020-12-20 07:19

    wrong name: org/kodeplay/kodejava/NewHelloWorld

    cd up to the package root, so that you're in the folder containing org folder and then do

    java -cp . org.kodeplay.kodejava.NewHelloWorld
    
    0 讨论(0)
  • 2020-12-20 07:21

    Go to the package root directory (the parent directory of org) and run:

    java -cp .:$CLASSPATH org.kodeplay.kodejava.NewHelloWorld

    Also I wouldn't put . to my CLASSPATH permanently (in .bashrc, .bash_profile or /etc/profile) it may lead to undesired behavior.

    0 讨论(0)
  • 2020-12-20 07:23

    The error message gives you a clue:

    (wrong name: org/kodeplay/kodejava/NewHelloWorld)
    

    It looks like your class is called org.kodeplay.kodejava.NewHelloWorld. The Java command line needs to know the fully qualified class name:

    java -cp . org.kodeplay.kodejava.NewHelloWorld
    

    should do the trick.

    0 讨论(0)
  • 2020-12-20 07:30

    I had a similar problem running a HelloWorld program I had written with a text editor on Mac OS X. It ran fine on a remote Linux box, but running it from home directory I got the dreaded NoClassDefFoundError.

    Found that I could fix it either by running as:

    java -cp . HelloWorld
    

    or, without the classpath qualifier, after adding the current directory to my bash CLASSPATH for the current session:

    export CLASSPATH=.
    
    0 讨论(0)
提交回复
热议问题