I am trying to run a Java application, but getting this error:
java.lang.ClassNotFoundException:
After the colon comes the location of the class that is missing. However, I know that that location does not exist since the class is located elsewhere. How can I update the path of that class? Does it have something to do with the class path?
Basic Generic Question - Simplest Generic Answer ;)
Given the information I will make the assumption that you might be trying a basic approach to coding, building/compiling and running a simple console app like "Hello World", using some simple text editor and some Command Shell.
This error occurs in the fallowing scenario:
In other words, use:
P.S. The adding the file extension .class produces the same mistake. Also be sure to have the Java's (JDK/JRE) bin folder in the operating system's Environment Variables's PATH.(Lookup for more details other posts on this) P.P.S Was I correct in my assumption/s?
To add the location of a class to your classpath via command line simply add
-cp
or-classpath
and the location of the class while running it. I.E.Or if you're running an IDE such as eclipse you can right click on the
project -> build path -> configure build path
and add the external JAR containing your class to the build path then it should work fine.If you know the path of the class or the jar containing the class then add it to your classpath while running it. You can use the classpath as mentioned here:
on Windows
on UNIX/Linux
This is the best solution I found so far.
Suppose we have a package called
org.mypackage
containing the classes: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:Use ';' as the separator. If your environment variables are set correctly, you should see your settings. If your PATH and CLASSPATH is correct, windows should recognize those commands. You do NOT need to restart your computer when installing Java.
It could happen if your classpath is not correct
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.