问题
I wrote a console application helloworld.java
and ran javac helloworld.java
,and then java helloworld
. This worked fine.
I then created a package com.abc.project
and moved helloworld.java into it(the package import statement is correctly generated which is package com.abc.project;
). And then i ran javac helloworld.java
this worked also fine and generated the class properly.
However, when I ran java com.abc.project.helloworld
from the console, it threw an "class not found" error.
Please can anyone advise what is the problem?
回答1:
Try running
java -cp ABSOLUTE_PATH com.abc.project.helloworld
Where ABSOLUTE_PATH refers to the directory where the class files along with packages are present. say it is bin
directory where the class files are generated along with same directory structure as source files
回答2:
First Please name Class with Capital Letter like HelloWorld.java
If you are in a folder '/myjava' in cmd and your .java files is in this folder then do this in cmd
D:\\myjava\:> javac -d HelloWorld.java
This will create correct package Structure for you Then don't go anywhere from the same location do this
D:\\myjava\:> java com.abc.project.HelloWorld
It should work fine!!
来源:https://stackoverflow.com/questions/22605831/how-to-resolve-this-error-caused-by-java-lang-classnotfoundexception