为什么 java -version 返回旧版本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1567242/
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-12 14:09:54  来源:igfitidea点击:

Why does java -version returns old version?

java

提问by Carlos Mu?oz

OS: Windows XP

操作系统:Windows XP

I am using yuicompressor-2.4.2 to compress some CSS before uploading to my server. But when I run it, the following exception appears:

我正在使用 yuicompressor-2.4.2 在上传到我的服务器之前压缩一些 CSS。但是当我运行它时,出现以下异常:

Exception in thread "main" java.lang.UnsupportedClassVersionError:
 com/yahoo/platform/yui/compressor/Bootstrap (Unsupported major.minor version 48.0)

So I think it's because of the JRE. The command I typed in cmd: java -version

所以我认为这是因为 JRE。我在cmd中输入的命令:java -version

And it says: java version "1.3.1_01" but should say "1.6.0_16" since I have installed the latest version.

它说:java版本“1.3.1_01”但应该说“1.6.0_16”,因为我已经安装了最新版本。

What should I do to make Java use the latest version instead of the old one?

我应该怎么做才能让 Java 使用最新版本而不是旧版本?

采纳答案by JuanZe

Set the environment variable JAVA_HOME pointing to the directory where you have jdk 1.6.0

设置环境变量 JAVA_HOME 指向你有 jdk 1.6.0 的目录

set JAVA_HOME=your_path_to_jdk1.6
set PATH=%JAVA_HOME%/bin;.;..;%PATH%

That's from a command window. Also you can do it from "My PC > Properties > Advanced > Environment variables"

那是从命令窗口。您也可以从“我的电脑 > 属性 > 高级 > 环境变量”中执行此操作

回答by Kevin

Add %JAVA_HOME%/bin to your PATH environment variable where JAVA_HOME is set to your JRE6u16 directory

将 %JAVA_HOME%/bin 添加到 PATH 环境变量中,其中 JAVA_HOME 设置为 JRE6u16 目录

回答by akf

You should check your PATH environment variable. It is possible that some application you have installed has put its version of the jre in front of yours in the path.

您应该检查您的 PATH 环境变量。您安装的某些应用程序可能已将其 jre 版本放在路径中您的前面。

回答by OscarRyz

You should modify your PATH environment variable:

您应该修改 PATH 环境变量:

My PC > Right click > properties > Advanced > Environment variables

我的电脑 > 右键单击​​ > 属性 > 高级 > 环境变量

And modify "Path"

并修改“路径”

Append at the end the path to your 1.6 installation:

在最后附加 1.6 安装的路径:

;C:\jdk1.6.xxx\bin

and remove the previous one if present.

并删除前一个(如果存在)。

回答by Michael Borgwardt

It looks like the older Java version is still on the system PATH environment variable (where the OS looks for commands) or JAVA_HOME (where yuicompressor may look for the java executable)

看起来较旧的 Java 版本仍在系统 PATH 环境变量(操作系统在其中查找命令)或 JAVA_HOME(在其中 yuicompressor 可能会查找 java 可执行文件)中

How those variables are changed depends on your operating system.

这些变量的更改方式取决于您的操作系统。

回答by erickson

On Windows, the JRE installs a javaexecutable in the Windows directory, which should be the first javain your path. This is just a wrapper that looks in the Windows Registry to find the Java home directory (should be "%SystemDrive%\Program Files\Java\jre6" for Java 6) and runs using the libraries there.

在 Windows 上,JRE 会java在 Windows 目录中安装一个可执行文件,该目录应该是java您路径中的第一个。这只是一个包装器,它在 Windows 注册表中查找 Java 主目录(对于 Java 6,应该是“%SystemDrive%\Program Files\Java\jre6”)并使用那里的库运行。

Run %SystemRoot%\system32\java -version and see what you get. If that isJava 6, you have entries in your path before %SystemRoot%\system32 (which really should be first). Either fix your %PATH% variable, or you'll have to be explicit whenever you want to run this version of Java.

运行 %SystemRoot%\system32\java -version 看看你得到了什么。如果那Java 6,你的路径中有 %SystemRoot%\system32 之前的条目(这应该是第一个)。要么修复您的 %PATH% 变量,要么在您想要运行此版本的 Java 时都必须明确。

If running that instance of javadoesn't report Java 6, its not installed (correctly). Uninstall and try installing again.

如果运行该实例java不报告 Java 6,则它未安装(正确)。卸载并再次尝试安装。

If you are having trouble because of the PATH, it is because you or some software you installed monkeyed with it; I recommend using the default which is to have system32 first. Everything works fine if the defaults are used.

如果您因为 PATH 而遇到问题,那是因为您或您安装的某些软件对其进行了修改;我建议使用默认值,即首先使用 system32。如果使用默认值,一切正常。

Also, %JAVA_HOME% is not used by the JRE itself at all.Some common Java applications like tomcat and ant honor the %JAVA_HOME% setting, so perhaps yuicompressor does too. But this is a de facto convention, not a standard.

此外,JRE 本身根本不使用 %JAVA_HOME% 一些常见的 Java 应用程序,如 tomcat 和 ant,遵循 %JAVA_HOME% 设置,因此 yuicompressor 也可能这样做。但这是事实上的约定,而不是标准。

回答by Waseem Akram Malik

Go to the system32 directory C:\Windows\System32 and delete following 3 files

进入system32目录C:\Windows\System32,删除以下3个文件

  • java.exe
  • javaw.exe
  • javaws.exe
  • 执行程序
  • 执行程序
  • 执行程序

Now create a JAVA_HOME environment variable with value={root path of your jdk installation} and add the path till bin folder of your jdk in the PATH environment variable.

现在创建一个 JAVA_HOME 环境变量,值={jdk 安装的根路径},并在 PATH 环境变量中添加路径直到 jdk 的 bin 文件夹。

Open a fresh command prompt and run java -version to confirm the change

打开新的命令提示符并运行 java -version 以确认更改

回答by Arundev

If you are using windows 7/10go to command prompt and type

如果您使用的是Windows 7/10,请转到命令提示符并键入

where java

Delete all the symbolic link shows below other than your actual installation directory.

除实际安装目录外,删除下面显示的所有符号链接。

Even if correctly added all the environment variables still sometime you can get wrong versions especially when you have multiple version installed in your system and want to switch between them.

即使正确添加了所有环境变量,有时您仍然会得到错误的版本,尤其是当您的系统中安装了多个版本并希望在它们之间切换时。

回答by Curious Tester

I had the same problem. In System Properties > Environment Variables > System Variables > PATHmake sure there is no other path associated with Java. To make sure, type in and check Java paths.

我有同样的问题。在System Properties > Environment Variables > System Variables > PATH确保有与Java相关的任何其他路径。为了确保,输入并检查 Java 路径。

where java

Re-open Command Prompt and type java -versionagain. I hope this helps.

重新打开命令提示符并java -version再次键入。我希望这有帮助。