java -version 抛出 “java.lang.ClassNotFoundException:”

huangapple go评论78阅读模式
英文:

java -version throws "java.lang.ClassNotFoundException: "

问题

我在Debian上使用OpenJDK 11。每当我尝试运行 java -version 时,我会得到以下错误:

错误:找不到或加载主类
原因:java.lang.ClassNotFoundException:

然而,像这样运行是可以的:

/usr/lib/jvm/java-11-openjdk-amd64/bin/java -version

我已经检查了符号链接,它们都工作正常。如果 javac 能够工作,为什么 java 就不行呢?因为它们都在同一个文件夹中。我漏掉了什么吗?

当我尝试运行任何已编译的类时,我也会得到相同的错误。最近我尝试过切换到不同的JDK,但失败了,在那之前我没有遇到过这个问题。我的 $PATH 和 $JAVA_HOME 都设置正确。

英文:

I am using OpenJDK 11 on Debian. Whenever I try to run java -version, I get this:

Error: Could not find or load main class 
Caused by: java.lang.ClassNotFoundException:

However, running like this works:

/usr/lib/jvm/java-11-openjdk-amd64/bin/java -version

I checked the symlinks, they all worked fine. If javac can work, why can't java, because both of them are in the same folder. What am I missing?

I get the same when trying to run any compiled classes. I recently tried (but failed) switching to a different JDK, before which I didn't have this problem. My $PATH and $JAVA_HOME are set correctly.

答案1

得分: 0

我终于找到了答案。我退了一步,并思考了我更改默认JDK时所采取的确切步骤。当我切换到13时,运行java会在我的屏幕上显示一堆初始设置,所以我按照某人的答案来抑制它,而这导致了错误。

我在我的.bash_profile中添加了这行代码来抑制它,将其注释掉,然后重新启动问题就解决了!

_SILENT_JAVA_OPTIONS="$_JAVA_OPTIONS" && unset _JAVA_OPTIONS &&  alias java='java "$_SILENT_JAVA_OPTIONS"'

专业提示:如果你遇到这样的错误,只需检查你可能搞乱了哪些环境变量。

英文:

I finally found the answer. I took a step back, and thought out the exact steps I took when I changed my default JDK. When I had switched to 13, running java would echo a bunch of init settings on my screen, so I followed somebody's answer to suppress it, and THAT caused the error.

I had added this line to my .bash_profile to suppress it, and commenting it out followed by a reboot just solved the problem!

_SILENT_JAVA_OPTIONS="$_JAVA_OPTIONS" && unset _JAVA_OPTIONS &&  alias java='java "$_SILENT_JAVA_OPTIONS"'

PRO-TIP: If you're getting such errors, simply check which Environment variables could you have messed up with.

huangapple
  • 本文由 发表于 2020年10月8日 05:39:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64252694.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定