How to resolve this error Caused by: java.lang.ClassNotFoundException

假装没事ソ 提交于 2019-12-31 03:31:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!