Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiler Error - 'Error: Could not find or load main class com.sun.tools.javac.Main'

I just started learning Java and I installed JDK on my computer, but now I am trying the SIMPLEST of Java and its not compiling. I installed JDK on C:/Java/jdk7/.

Whenever I try to compile, I get an error:

Error: Could not find or load main class com.sun.tools.javac.Main

Here is how I'm compiling:

javac test.java

I also tried:

javac.exe test.java

I don't know if my code is wrong or anything, but here is my test.java:

class test {
    public static void main(String args[]) {
        System.out.println("Hello World!");
    }
}

Here is JAVA_HOME:

C:\Java\jdk7\

Any help would be appreciated!

like image 695
Oliver Ni Avatar asked Dec 15 '13 00:12

Oliver Ni


People also ask

Could not find or load main class cmd error?

The error 'Could not find or load main class' occurs when using a java command in the command prompt to launch a Java program by specifying the class name in the terminal. The reason why this happens is mostly due to the user's programming mistake while declaring the class.

Why Javac is not recognized in CMD?

When we type the command in the Command Prompt, the prompt refuse to identify the javac command. It means that the javac.exe file is not found by the compiler. The javac.exe file exists in the bin folder of the JDK installation folder. The error we get because the PATH is not properly set.

Could not find the main class main program will exit?

Problem: Error "Could not find the main class - program will exit" on the ServiceDesk configuration while configuring Web Services. Cause: This happens when there are multiple java versions installed and the value of NtfsDisable8dot3NameCreation is incorrectly set.


2 Answers

You probably have done a manual installation of JDK. Anyway, this error is almost certainly due to a flaw in your Java installation. In order to solve it, you must execute the following command in your JAVA_HOME/lib directory:

unpack200 -r -v -l "" tools.pack tools.jar

This will unpack the tools.jar file, which your installation (manual or not) had not done for you. After that try to execute:

javac -version

This command should work well. This is similar to the error you can have with the Java command, for basically the same reason, your installation didn't unpack necessary files. You can refer to this link: JRE 1.7 returns: java/lang/NoClassDefFoundError: java/lang/Object

I had this problem myself and my solution is a little adaptation of this other answer.

like image 140
Aureliano Buendia Avatar answered Oct 03 '22 22:10

Aureliano Buendia


Did you reboot after you installed? There are some important environment variables (namely the CLASSPATH) that aren't set until you reboot Windows. Anyway, you can work around it by adding rt.jar and tools.jar to your CLASSPATH. Also, you should probably make sure your JAVA_HOME is set.

like image 21
Elliott Frisch Avatar answered Oct 03 '22 22:10

Elliott Frisch