How do I resolve ClassNotFoundException?

前端 未结 22 1986
后悔当初
后悔当初 2020-11-21 06:06

I am trying to run a Java application, but getting this error:

java.lang.ClassNotFoundException:

After the colon comes the location of the cla

22条回答
  •  深忆病人
    2020-11-21 06:22

    This is the best solution I found so far.

    Suppose we have a package called org.mypackage containing the classes:

    • HelloWorld (main class)
    • SupportClass
    • UtilClass

    and the files defining this package are stored physically under the directory D:\myprogram (on Windows) or /home/user/myprogram (on Linux).

    The file structure will look like this:

    When we invoke Java, we specify the name of the application to run: org.mypackage.HelloWorld. However we must also tell Java where to look for the files and directories defining our package. So to launch the program, we have to use the following command:

    NOTE: You have to execute the above java command no matter what your current location is. But this is not the case for javac. For compiling you can even directly go into the directory where you have your .java files and directly execute javac ClassName.java.

提交回复
热议问题