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

Viewed 61973

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!

7 Answers

First make sure that there is a tools.jar in your jdk\lib folder.

If so then follow these steps:

  1. Execute the following code in your command prompt in your jdk directory

    for %I in (.) do echo %~sI
    

Even if you are not in the jdk directory just add the name of the directory instead of "." . This code will return you the dos path. Copy paste the dos path in JAVA_HOME.

  1. Run the above code again when you are in JAVA_HOME\lib. Copy paste that into the environment variable named CLASSPATH. Just add "\tools.jar" at the end.

Even if it doesn't help try reinstalling Java (or just extract the tools.jar file for cross check). Hope it helps

I have many versions of JDK on my laptop, today I encountered this problem: After I switched to JDK1.7 to my JAVA_HOME, I typed javac in the cmd and got that error. At last, I uninstalled JDK1.7 and reinstalled it, and the issue had gone.

Related