编译器错误 - “错误:无法找到或加载主类 com.sun.tools.javac.Main”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20589718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:58:20  来源:igfitidea点击:

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

javacompiler-errors

提问by Oliver Ni

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

我刚开始学习 Java 并在我的计算机上安装了 JDK,但现在我正在尝试 Java 的 SIMPLIST 并且它没有编译。我在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:

我不知道我的代码是错误还是什么,但这是我的test.java

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

Here is JAVA_HOME:

这是JAVA_HOME:

C:\Java\jdk7\

Any help would be appreciated!

任何帮助,将不胜感激!

采纳答案by Aureliano Buendia

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:

您可能已经手动安装了 JDK。无论如何,这个错误几乎肯定是由于您的 Java 安装中的缺陷造成的。为了解决它,您必须在您的 JAVA_HOME/lib 目录中执行以下命令:

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

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:

这将解压 tools.jar 文件,这是您的安装(手动或非手动)没有为您完成的。之后尝试执行:

javac -version

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

此命令应该可以正常工作。这类似于您使用 Java 命令可能遇到的错误,出于基本相同的原因,您的安装没有解压缩必要的文件。可以参考这个链接:JRE 1.7返回:java/lang/NoClassDefFoundError:java/lang/Object

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

我自己也遇到了这个问题,我的解决方案是对另一个答案的一些改编。

回答by Elliott Frisch

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.jarand tools.jarto your CLASSPATH. Also, you should probably make sure your JAVA_HOMEis set.

你安装后重启了吗?有一些重要的环境变量(即CLASSPATH)在您重新启动 Windows 之前不会设置。无论如何,您可以通过将rt.jar和添加tools.jar到您的CLASSPATH. 此外,您应该确保JAVA_HOME已设置好。

回答by t_ozawa

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\"(e.g) to the path variable.
(See http://www.computerhope.com/issues/ch000549.htm)

错误日志意味着找不到包含 tools.jar 的类。Tools.jar 始终包含在 JDK 中。所以,我猜你使用了错误的 JDK。所以,你试着写你安装的javac全路径。
示例) "C:\Program Files\java\jdk1.7.0_25\bin\javac" test.java (windows)

如果成功了,说明路径变量中的JDK是错误的。因此,您将路径“C:\Program Files\java\jdk1.7.0_25\bin\”(例如)添加到路径变量中。
(见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.

如果它不起作用,请检查 ...\jdk1.7.0_25\lib\tools.jar。如果它不存在,那就错了。所以,你重试安装。
如果它存在,嗯,我不知道。我建议你重新安装。

回答by Suraj

I faced the issue on JDK1.7_67.

我在 JDK1.7_67 上遇到了这个问题。

I solved it by seperating JDK and JRE installations.

我通过分离 JDK 和 JRE 安装解决了它。

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.

JDK 安装在 D:\Java\jdk1.7_67 并带有自己的 jre/lib 文件夹,JRE 安装在 D:\Java\jre1.7_67 带有自己的 lib 文件夹。

This solved the tools.jar issue for me.

这为我解决了 tools.jar 问题。

回答by Ranshu

"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

jdk 的 bin 文件夹中缺少“javac.exe”。检查它,如果它不存在,那么只需从您朋友的任何也在使用 java 的计算机的 java/jdk/bin 文件夹中复制“javac.exe”文件。我希望它会帮助你。谢谢

回答by Koushik Kumar Dey

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

首先确保你的 .jar 文件中有一个 tools.jar jdk\lib folder

If so then follow these steps:

如果是这样,请按照以下步骤操作:

  1. Execute the following code in your command prompt in your jdkdirectory

    for %I in (.) do echo %~sI
    
  1. 在您的jdk目录中的命令提示符中执行以下代码

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

Even if you are not in the jdkdirectory 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.

即使您不在jdk目录中,只需添加目录名称而不是“。” . 此代码将返回 dos 路径。将 dos 路径复制粘贴到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.
  1. 进入后再次运行上面的代码JAVA_HOME\lib。将其复制粘贴到名为CLASSPATH. 加"\tools.jar"在最后就好了。

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

即使它没有帮助尝试重新安装 Java(或者只是提取 tools.jar 文件进行交叉检查)。希望能帮助到你

回答by Eric Ben

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

我的JDK笔记本电脑上有很多版本,今天我遇到了这个问题:在我切换JDK1.7到我的 cmd 后JAVA_HOME,我输入javac了 cmd 并得到了那个错误。最后,我卸载JDK1.7并重新安装它,问题消失了。