How do I resolve ClassNotFoundException?

前端 未结 22 1281
后悔当初
后悔当初 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.

    0 讨论(0)
  • 2020-11-21 06:23

    Try these if you use maven. I use maven for my project and when I do mvn clean install and try to run a program it throws the exception. So, I clean the project and run it again and it works for me.

    I use eclipse IDE.

    For Class Not Found Exception when running Junit test, try running mvn clean test once. It will compile all the test classes.

    0 讨论(0)
  • 2020-11-21 06:23

    I started having this issue after upgrading the "Java Language Support" plugin from Visual Studio Code from version 0.66.0 to 0.67.0.

    Downgrading back allowed me to run the same code without any issue.

    0 讨论(0)
  • 2020-11-21 06:24
    1. It could happen if your classpath is not correct

    2. Let us posit a serializable class and deserializable class under same projectname. You run the serializable class, creating a serializable object in specific folder. Now you need the desearialized data. In the meantime, if you change the name of the project it will not work. You have to run the serializable class first and then deserialize the file.

    0 讨论(0)
  • 2020-11-21 06:25

    Your classpath is broken (which is a very common problem in the Java world).

    Depending on how you start your application, you need to revise the argument to -cp, your Class-Path entry in MANIFEST.MF or your disk layout.

    0 讨论(0)
  • 2020-11-21 06:25

    Go up to the top and remove the import statement if there is one, and re import the class. But if that isn't the case do a clean then build. Are you using Netbeans or Eclipse?

    0 讨论(0)
提交回复
热议问题