为什么Java SDK安装程序没有设置JAVA_HOME?

时间:2022-06-13 10:56:31

I always wondered why I have to manually set the JAVA_HOME environment variable after installing the Java SDK.

我总是想知道为什么我必须在安装Java SDK后手动设置JAVA_HOME环境变量。

JAVA_HOME=c:\Program Files\Java\jdk1.6.0_12

Visual Studio at least provides a batch file to set these kind of environment variables:

Visual Studio至少提供了一个批处理文件来设置这些环境变量:

call "c:\Program Files\Microsoft Visual Studio 9.0\VC\vcvarsall.bat"

调用“c:\ Program Files \ Microsoft Visual Studio 9.0 \ VC \ vcvarsall.bat”

Does Java have something similar? I'm trying to make a build script that should simply work after installing the Java SDK. I don't want people to mess with environment variables on their PC.

Java有类似的东西吗?我正在尝试制作一个构建脚本,它应该在安装Java SDK之后才能工作。我不希望人们在PC上弄乱环境变量。

6 个解决方案

#1


33  

You can install as many versions of Java as you like.

您可以根据需要安装任意数量的Java版本。

It would be dangerous for the setup to modify a local environment variable such as JAVA_HOME, since it might reference an existing Java installation.

设置修改本地环境变量(如JAVA_HOME)会很危险,因为它可能引用现有的Java安装。

This has nothing to do with an alleged "platform dependent issue". ;)

这与所谓的“平台相关问题”无关。 ;)

Since scripts might depend on JAVA_HOME to launch themselves, again, this would be disasterous for a new Java install to modify JAVA_HOME: all those scripts would suddenly have to be launched with a new potentially incompatible JVM.

由于脚本可能依赖于JAVA_HOME进行自我启动,因此对于新的Java安装来修改JAVA_HOME将是一件灾难:所有这些脚本突然必须使用新的可能不兼容的JVM启动。

Plus, by setting $JAVA_HOME/bin or %JAVA_HOME%/bin in your path, you can dynamically change JAVA_HOME to whatever version of Java you want to use without having to much with your PATH variable.

另外,通过在路径中设置$ JAVA_HOME / bin或%JAVA_HOME%/ bin,您可以动态地将JAVA_HOME更改为您想要使用的任何Java版本,而无需使用PATH变量。


Michael Borgwardt has made in the comments the interesting followup's question

Michael Borgwardt在评论中提出了有趣的后续问题

Still, this does not explain why the installer does not set JAVA_HOME when it is not previously set at all.

尽管如此,这并不能解释为什么安装程序在以前没有设置的情况下也没有设置JAVA_HOME。

The answer is simple:

答案很简单:

The setup cannot know if a script already depends on JAVA_HOME or not.

设置无法知道脚本是否已经依赖于JAVA_HOME。

Meaning: some scripts could test for JAVA_HOME value, and if not set, refer to another JVM installed elsewhere (and do not forget that by "install", one can only refer to "copied": a JDK/JRE is not always installed by a setup)

含义:某些脚本可以测试JAVA_HOME值,如果没有设置,请参考其他地方安装的另一个JVM(不要忘记通过“install”,只能引用“复制”:JDK / JRE并不总是安装在设置)

If you set JAVA_HOME, that can disrupt the default behavior of some of your scripts.

如果设置JAVA_HOME,则可能会破坏某些脚本的默认行为。

Not wanting to disturb hypothetical scripts that depend on a env var not being set sound pointlessly paranoid to me - If a script does that, then it clearly WANTS to use a different JVM when one is installed - no reason to avoid that.

不想打扰依赖于env var而没有设置声音的假设脚本对我来说毫无意义的偏执 - 如果脚本这样做,那么在安装一个JVM时它显然要使用不同的JVM - 没有理由避免这种情况。

Mmm... Sweet. For dealing with massive deployment issues on a daily-basis (for internal application in my shop), I can assure you: it is very sane "paranoid" treat to have.
When you deploy to a (very) large set of users, you do not want to make any assumption about their platform and configurations. "clearly WANTS" is an assumption I would not dare to make (or I redirect my phone to yours ;) and you handle the angry calls).

嗯......很好。为了每天处理大量的部署问题(对于我店里的内部应用程序),我可以向你保证:这是非常理智的“偏执”待遇。当您部署到(非常)大型用户集时,您不希望对其平台和配置做出任何假设。 “显然WANTS”是我不敢做的假设(或者我将我的手机重定向到你的手机;)并且你处理了愤怒的电话。

For instance, we have many scripts which launches with a 1.4.2 JVM from sun (JAVA_HOME not set on development platform, default path set directly in the script), or with 1.4.2 from JRockit (JAVA_HOME set, as it is the intended target on integration, pre-production and production platforms).

例如,我们有许多脚本使用来自sun的1.4.2 JVM启动(JAVA_HOME未在开发平台上设置,默认路径直接在脚本中设置),或者使用来自JRockit的1.4.2(JAVA_HOME设置,因为它是预期的)整合,预生产和生产平台的目标)。

But we install regularly the new JDK1.6.x since we use it for launching eclipse.

但是我们定期安装新的JDK1.6.x,因为我们用它来启动eclipse。

Assume that those scripts want their JAVA_HOME set... and nothing works anymore.

假设那些脚本想要他们的JAVA_HOME设置......并且没有任何东西可以工作了。

... To which Robert Grant makes this on-the-spot critic:

......罗伯特·格兰特(Robert Grant)对此进行了现场评论:

You're describing scripts that require one specific version, but still look at global JAVA_HOME. That's just badly thought out scripts.

您正在描述需要一个特定版本的脚本,但仍然要查看全局JAVA_HOME。这只是经过深思熟虑的脚本。

While that may or may not be true, that also illustrates precisely my point:
"you do not want to make any assumption": no assumption on their platform/settings, and no assumption on their "best practices".
The former may sound paranoid, the latter is plain common-sense: thinking that your product (here a JDK setup) will not break anything on the user's environment because the user has "correctly" thought out his scripts... would be insane.

虽然这可能或可能不是真的,但这也恰恰说明了我的观点:“你不想做出任何假设”:对他们的平台/设置没有假设,也没有假设他们的“最佳实践”。前者可能听起来很偏执,后者是普通常识:认为你的产品(这里是一个JDK设置)不会破坏用户环境中的任何东西,因为用户“正确”地想出了他的脚本......会疯了。


GvS suggests:

Or it could just have option to do it, disabled by default

或者它可以选择执行此操作,默认情况下禁用

That would mean another option to include in the setup screens, option which should be carefully reviewed by the user, and which may have unintended consequences, even when the user selects it thinking he knows what he is doing...

这意味着在设置屏幕中包含另一个选项,应该由用户仔细审查,并且可能会产生意想不到的后果,即使用户选择它认为他知道自己在做什么......

It is simply not worth it.

这根本不值得。

#2


10  

I don't think JAVA_HOME is a convention invented or supported by Sun.

我不认为JAVA_HOME是Sun发明或支持的约定。

They probably remember the fiasco that was the CLASSPATH environment variable** all too well and prefer to stay the hell away from environment variables.

他们可能还记得CLASSPATH环境变量**的惨败,并且更喜欢让它远离环境变量。

** This was encouraged as the primary way to set the JVM classpath in earlier Java SDKs and literature, and resulted in the user and various applications messing with the environment variable, overwriting each other's changes and depending on mutually contradictive contents.

**鼓励这是在早期Java SDK和文献中设置JVM类路径的主要方法,并导致用户和各种应用程序弄乱环境变量,覆盖彼此的更改并依赖于相互矛盾的内容。

#3


3  

The vcvarsall.bat mechanism is a convenient way for Visual C++ to provide a console with the correct variables without messing with the user's/system's environment variables. However, it assumes that Installshield is the only way to get code onto the system. The JDK should tolerate being cut'n'pasted from one location to another.

vcvarsall.bat机制是Visual C ++为控制台提供正确变量而不会弄乱用户/系统环境变量的便捷方式。但是,它假定Installshield是将代码放入系统的唯一方法。 JDK应该容忍从一个位置切换到另一个位置。

If you're looking for java.exe, the Installshield installer should put it in %windir%\system32, so it is available on the PATH.

如果您正在寻找java.exe,Installshield安装程序应将其放在%windir%\ system32中,因此它在PATH上可用。

You can gain some hints about the location of installed apps by querying the registry:

您可以通过查询注册表获得有关已安装应用程序位置的一些提示:

C:>REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit\1.6" /v JavaHome

! REG.EXE VERSION 3.0

HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit\1.6
    JavaHome    REG_SZ  C:\dev\Java\jdk1.6.0_05

However, you can't rely on this absolutely because this makes some assumptions about vendor, version and installation mechanism.

但是,您绝对不能依赖于此,因为这会对供应商,版本和安装机制做出一些假设。

#4


1  

This may help someone else out who ends up here like me. I just want to use Java as a tool, not adopt it as a way of life, so I only needed to know how JAVA_HOME was getting set and why it was not correct. The answer turned out to be that the WinAnt installation sets JAVA_HOME (along with ANT_HOME), but only based on the currently installed Java. So if you need to change the version of Java, and you are using Ant, the correct way to do it is to uninstall WinAnt, uninstall Java, install the new Java, and then reinstall WinAnt.

这可能会帮助其他人像我一样在这里结束。我只想使用Java作为工具,而不是将其作为一种生活方式,所以我只需要知道JAVA_HOME是如何设置的以及为什么它不正确。答案结果是WinAnt安装设置了JAVA_HOME(以及ANT_HOME),但仅基于当前安装的Java。因此,如果您需要更改Java的版本,并且您正在使用Ant,那么正确的方法是卸载WinAnt,卸载Java,安装新的Java,然后重新安装WinAnt。

#5


0  

I'm not sure why this is, because the installers clearly solve platform dependant issues (which is ofcourse the whole point of a JVM). Are you sure you aren't mixing the JRE with the JSDK?

我不确定为什么会这样,因为安装人员清楚地解决了平台相关的问题(当然这是JVM的全部要点)。你确定你没有将JRE与JSDK混合使用吗?

Maybe there's a way for your program to search where java is installed (that would be a script I guess), and then set JAVA_HOME and possibly add it to the path.

也许你的程序有一种方法可以搜索安装java的地方(我猜这是一个脚本),然后设置JAVA_HOME并可能将其添加到路径中。

IBM seems to be doing this trick already: http://www-01.ibm.com/support/docview.wss?rs=180&uid=swg21199220

IBM似乎已经在做这个技巧:http://www-01.ibm.com/support/docview.wss?rs = 180&uid = swg21199220

Other interesting post hinting at the difference between JRE and JSDK installations: http://confluence.atlassian.com/display/CONF26/Set+JAVA_HOME+variable+in+Windows

其他有趣的帖子暗示了JRE和JSDK安装之间的区别:http://confluence.atlassian.com/display/CONF26/Set+JAVA_HOME+variable+in+Windows

Hope this helps.

希望这可以帮助。

#6


-1  

I guess java doesn't want to do anything which is platform-dependent. In Windows, classpaths are set differently from LINUX/UNIX.

我想java不想做任何与平台相关的事情。在Windows中,类路径的设置与LINUX / UNIX不同。

#1


33  

You can install as many versions of Java as you like.

您可以根据需要安装任意数量的Java版本。

It would be dangerous for the setup to modify a local environment variable such as JAVA_HOME, since it might reference an existing Java installation.

设置修改本地环境变量(如JAVA_HOME)会很危险,因为它可能引用现有的Java安装。

This has nothing to do with an alleged "platform dependent issue". ;)

这与所谓的“平台相关问题”无关。 ;)

Since scripts might depend on JAVA_HOME to launch themselves, again, this would be disasterous for a new Java install to modify JAVA_HOME: all those scripts would suddenly have to be launched with a new potentially incompatible JVM.

由于脚本可能依赖于JAVA_HOME进行自我启动,因此对于新的Java安装来修改JAVA_HOME将是一件灾难:所有这些脚本突然必须使用新的可能不兼容的JVM启动。

Plus, by setting $JAVA_HOME/bin or %JAVA_HOME%/bin in your path, you can dynamically change JAVA_HOME to whatever version of Java you want to use without having to much with your PATH variable.

另外,通过在路径中设置$ JAVA_HOME / bin或%JAVA_HOME%/ bin,您可以动态地将JAVA_HOME更改为您想要使用的任何Java版本,而无需使用PATH变量。


Michael Borgwardt has made in the comments the interesting followup's question

Michael Borgwardt在评论中提出了有趣的后续问题

Still, this does not explain why the installer does not set JAVA_HOME when it is not previously set at all.

尽管如此,这并不能解释为什么安装程序在以前没有设置的情况下也没有设置JAVA_HOME。

The answer is simple:

答案很简单:

The setup cannot know if a script already depends on JAVA_HOME or not.

设置无法知道脚本是否已经依赖于JAVA_HOME。

Meaning: some scripts could test for JAVA_HOME value, and if not set, refer to another JVM installed elsewhere (and do not forget that by "install", one can only refer to "copied": a JDK/JRE is not always installed by a setup)

含义:某些脚本可以测试JAVA_HOME值,如果没有设置,请参考其他地方安装的另一个JVM(不要忘记通过“install”,只能引用“复制”:JDK / JRE并不总是安装在设置)

If you set JAVA_HOME, that can disrupt the default behavior of some of your scripts.

如果设置JAVA_HOME,则可能会破坏某些脚本的默认行为。

Not wanting to disturb hypothetical scripts that depend on a env var not being set sound pointlessly paranoid to me - If a script does that, then it clearly WANTS to use a different JVM when one is installed - no reason to avoid that.

不想打扰依赖于env var而没有设置声音的假设脚本对我来说毫无意义的偏执 - 如果脚本这样做,那么在安装一个JVM时它显然要使用不同的JVM - 没有理由避免这种情况。

Mmm... Sweet. For dealing with massive deployment issues on a daily-basis (for internal application in my shop), I can assure you: it is very sane "paranoid" treat to have.
When you deploy to a (very) large set of users, you do not want to make any assumption about their platform and configurations. "clearly WANTS" is an assumption I would not dare to make (or I redirect my phone to yours ;) and you handle the angry calls).

嗯......很好。为了每天处理大量的部署问题(对于我店里的内部应用程序),我可以向你保证:这是非常理智的“偏执”待遇。当您部署到(非常)大型用户集时,您不希望对其平台和配置做出任何假设。 “显然WANTS”是我不敢做的假设(或者我将我的手机重定向到你的手机;)并且你处理了愤怒的电话。

For instance, we have many scripts which launches with a 1.4.2 JVM from sun (JAVA_HOME not set on development platform, default path set directly in the script), or with 1.4.2 from JRockit (JAVA_HOME set, as it is the intended target on integration, pre-production and production platforms).

例如,我们有许多脚本使用来自sun的1.4.2 JVM启动(JAVA_HOME未在开发平台上设置,默认路径直接在脚本中设置),或者使用来自JRockit的1.4.2(JAVA_HOME设置,因为它是预期的)整合,预生产和生产平台的目标)。

But we install regularly the new JDK1.6.x since we use it for launching eclipse.

但是我们定期安装新的JDK1.6.x,因为我们用它来启动eclipse。

Assume that those scripts want their JAVA_HOME set... and nothing works anymore.

假设那些脚本想要他们的JAVA_HOME设置......并且没有任何东西可以工作了。

... To which Robert Grant makes this on-the-spot critic:

......罗伯特·格兰特(Robert Grant)对此进行了现场评论:

You're describing scripts that require one specific version, but still look at global JAVA_HOME. That's just badly thought out scripts.

您正在描述需要一个特定版本的脚本,但仍然要查看全局JAVA_HOME。这只是经过深思熟虑的脚本。

While that may or may not be true, that also illustrates precisely my point:
"you do not want to make any assumption": no assumption on their platform/settings, and no assumption on their "best practices".
The former may sound paranoid, the latter is plain common-sense: thinking that your product (here a JDK setup) will not break anything on the user's environment because the user has "correctly" thought out his scripts... would be insane.

虽然这可能或可能不是真的,但这也恰恰说明了我的观点:“你不想做出任何假设”:对他们的平台/设置没有假设,也没有假设他们的“最佳实践”。前者可能听起来很偏执,后者是普通常识:认为你的产品(这里是一个JDK设置)不会破坏用户环境中的任何东西,因为用户“正确”地想出了他的脚本......会疯了。


GvS suggests:

Or it could just have option to do it, disabled by default

或者它可以选择执行此操作,默认情况下禁用

That would mean another option to include in the setup screens, option which should be carefully reviewed by the user, and which may have unintended consequences, even when the user selects it thinking he knows what he is doing...

这意味着在设置屏幕中包含另一个选项,应该由用户仔细审查,并且可能会产生意想不到的后果,即使用户选择它认为他知道自己在做什么......

It is simply not worth it.

这根本不值得。

#2


10  

I don't think JAVA_HOME is a convention invented or supported by Sun.

我不认为JAVA_HOME是Sun发明或支持的约定。

They probably remember the fiasco that was the CLASSPATH environment variable** all too well and prefer to stay the hell away from environment variables.

他们可能还记得CLASSPATH环境变量**的惨败,并且更喜欢让它远离环境变量。

** This was encouraged as the primary way to set the JVM classpath in earlier Java SDKs and literature, and resulted in the user and various applications messing with the environment variable, overwriting each other's changes and depending on mutually contradictive contents.

**鼓励这是在早期Java SDK和文献中设置JVM类路径的主要方法,并导致用户和各种应用程序弄乱环境变量,覆盖彼此的更改并依赖于相互矛盾的内容。

#3


3  

The vcvarsall.bat mechanism is a convenient way for Visual C++ to provide a console with the correct variables without messing with the user's/system's environment variables. However, it assumes that Installshield is the only way to get code onto the system. The JDK should tolerate being cut'n'pasted from one location to another.

vcvarsall.bat机制是Visual C ++为控制台提供正确变量而不会弄乱用户/系统环境变量的便捷方式。但是,它假定Installshield是将代码放入系统的唯一方法。 JDK应该容忍从一个位置切换到另一个位置。

If you're looking for java.exe, the Installshield installer should put it in %windir%\system32, so it is available on the PATH.

如果您正在寻找java.exe,Installshield安装程序应将其放在%windir%\ system32中,因此它在PATH上可用。

You can gain some hints about the location of installed apps by querying the registry:

您可以通过查询注册表获得有关已安装应用程序位置的一些提示:

C:>REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit\1.6" /v JavaHome

! REG.EXE VERSION 3.0

HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit\1.6
    JavaHome    REG_SZ  C:\dev\Java\jdk1.6.0_05

However, you can't rely on this absolutely because this makes some assumptions about vendor, version and installation mechanism.

但是,您绝对不能依赖于此,因为这会对供应商,版本和安装机制做出一些假设。

#4


1  

This may help someone else out who ends up here like me. I just want to use Java as a tool, not adopt it as a way of life, so I only needed to know how JAVA_HOME was getting set and why it was not correct. The answer turned out to be that the WinAnt installation sets JAVA_HOME (along with ANT_HOME), but only based on the currently installed Java. So if you need to change the version of Java, and you are using Ant, the correct way to do it is to uninstall WinAnt, uninstall Java, install the new Java, and then reinstall WinAnt.

这可能会帮助其他人像我一样在这里结束。我只想使用Java作为工具,而不是将其作为一种生活方式,所以我只需要知道JAVA_HOME是如何设置的以及为什么它不正确。答案结果是WinAnt安装设置了JAVA_HOME(以及ANT_HOME),但仅基于当前安装的Java。因此,如果您需要更改Java的版本,并且您正在使用Ant,那么正确的方法是卸载WinAnt,卸载Java,安装新的Java,然后重新安装WinAnt。

#5


0  

I'm not sure why this is, because the installers clearly solve platform dependant issues (which is ofcourse the whole point of a JVM). Are you sure you aren't mixing the JRE with the JSDK?

我不确定为什么会这样,因为安装人员清楚地解决了平台相关的问题(当然这是JVM的全部要点)。你确定你没有将JRE与JSDK混合使用吗?

Maybe there's a way for your program to search where java is installed (that would be a script I guess), and then set JAVA_HOME and possibly add it to the path.

也许你的程序有一种方法可以搜索安装java的地方(我猜这是一个脚本),然后设置JAVA_HOME并可能将其添加到路径中。

IBM seems to be doing this trick already: http://www-01.ibm.com/support/docview.wss?rs=180&uid=swg21199220

IBM似乎已经在做这个技巧:http://www-01.ibm.com/support/docview.wss?rs = 180&uid = swg21199220

Other interesting post hinting at the difference between JRE and JSDK installations: http://confluence.atlassian.com/display/CONF26/Set+JAVA_HOME+variable+in+Windows

其他有趣的帖子暗示了JRE和JSDK安装之间的区别:http://confluence.atlassian.com/display/CONF26/Set+JAVA_HOME+variable+in+Windows

Hope this helps.

希望这可以帮助。

#6


-1  

I guess java doesn't want to do anything which is platform-dependent. In Windows, classpaths are set differently from LINUX/UNIX.

我想java不想做任何与平台相关的事情。在Windows中,类路径的设置与LINUX / UNIX不同。