Could not find or load main class JAVA : RESOLVED

Have you ever run into an error during your JAVA program execution like "could not find or load main class XX" where XX is the name of your class. The scenario usually takes place when you are doing the compilation and execution through the command line interface (in Windows/Unix Operating System) . 

The program compiles correctly but doesn't executes and JVM throws error "could not find or load main class" . In this article i'll show you how to resolve this error.
The problem arises when the CLASSPATH variable is not properly set. The solution to this problem is to set the CLASSPATH envirponment variable either through command line(overriding the default classpath) or set a default classpath(when it is not set).
                                 

Setting the CLASSPATH Environment Variable : 


1. In Windows, click the start button and search for environment variable or follow My Computer > properties > Advanced System Settings (on left hand side) and then click the environment variables button.


2. Under the user variables for hp : select the CLASSPATH variable (if exists) or create a variable using New button as CLASSPATH and click on edit button.

3. Add the path of the folder containing your program for example my program was on desktop so i added "C:\Users\hp\Desktop" (you can enter your own path of your development folder in this variable) and click OK button.

4. Run the program and it will run successfully.

Setting the CLASSPATH through command line :


The preferred way to specify the class path is by using the -cp command line switch. This allows the CLASSPATH to be set individually for each application without affecting other applications.Setting the CLASSPATH can be tricky and should be performed with care.
To set the CLASSPATH variable through command line type the following command


java -classpath c:\Users\hp\Desktop Test

where "c:\Users\hp\desktop" is path (Remember the path is case-sensitive) and Test is the class name.
This command tells the JVM to look for the class in this path and override any default classpath which exists.By default, Java CLASSPATH points to current directory denoted by  and it will look for any class only in the current directory.
Note : This will not make a permanent environment variable and will work as long as you don't close the terminal or command line.

EASY WAY : 

Note there is an easy way where you don't need to worry about any CLASSPATH variables or any other thing. You can use any of the famous IDEs for JAVA development like eclipse ,netbeans etc. These applications take all the headache for you and will build the paths internally and you do not need to specify anything.



Thanks for Reading

1 comment:

  1. TITIAN LAS VEGAS LIMITED, UNITED STATES
    › product › stiletto titanium hammer titian-las-vegas- › product › titian-las-vegas- TITIAN LAS VEGAS LIMITED, leatherman charge titanium UNITED titanium daith jewelry STATES. TITIAN 2013 ford focus titanium hatchback LAS VEGAS LIMITED, UNITED STATES. TITIAN LAS VEGAS LIMITED, UNITED STATES. TITIAN LAS VEGAS trekz titanium pairing

    ReplyDelete

Powered by Blogger.