简体   繁体   中英

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!

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.

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.

Your source code is OK.

Error log means could not find the class included tools.jar. Tools.jar is always included JDK. So, I guess you use a wrong JDK. So, you try to write javac full path you installed.
example) "C:\\Program Files\\java\\jdk1.7.0_25\\bin\\javac" test.java (windows)

If it has worked, JDK in the path variable is wrong. So, you add the path "C:\\Program Files\\java\\jdk1.7.0_25\\bin\\"(eg) to the path variable.
(See http://www.computerhope.com/issues/ch000549.htm )

If it doesn't worked, you check ...\\jdk1.7.0_25\\lib\\tools.jar. If it doesn't exists, it's wrong. So, you retry to install.
If it exists, uhmmm, I don't know. I recommend re-install to you.

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.

I faced the issue on JDK1.7_67.

I solved it by seperating JDK and JRE installations.

JDK installed in D:\\Java\\jdk1.7_67 with its own jre/lib folder and JRE installed at D:\\Java\\jre1.7_67 with its own lib folder.

This solved the tools.jar issue for me.

"javac.exe" is missing from your bin folder of jdk. Check for it, if it is not there then Simply copy "javac.exe" file from the java/jdk/bin folder of any of your friend's computer who are also using java. I hope it will help you. Thank you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM