問(wèn)題描述
我一直在想,為什么我在安裝 Java SDK 后必須手動(dòng)設(shè)置 JAVA_HOME 環(huán)境變量.
<塊引用>JAVA_HOME=c:Program FilesJavajdk1.6.0_12
Visual Studio 至少提供了一個(gè)批處理文件來(lái)設(shè)置這些環(huán)境變量:
<塊引用>調(diào)用c:Program FilesMicrosoft VisualStudio 9.0VCvcvarsall.bat"
Java 有類(lèi)似的東西嗎?我正在嘗試制作一個(gè)在安裝 Java SDK 后應(yīng)該可以正常工作的構(gòu)建腳本.我不希望人們?cè)谒麄兊?PC 上弄亂環(huán)境變量.
您可以安裝任意多個(gè)版本的 Java.
安裝程序修改 本地 環(huán)境變量(例如 JAVA_HOME
)會(huì)很危險(xiǎn),因?yàn)樗赡軙?huì)引用現(xiàn)有的 Java 安裝.
這與所謂的平臺(tái)相關(guān)問(wèn)題"無(wú)關(guān).;)
由于腳本可能依賴(lài)于 JAVA_HOME
來(lái)啟動(dòng)自己,再次,這對(duì)于新的 Java 安裝來(lái)修改 JAVA_HOME
將是災(zāi)難性的:所有這些腳本都必須突然使用新的可能不兼容的 JVM 啟動(dòng).
另外,通過(guò)在路徑中設(shè)置 $JAVA_HOME/bin
或 %JAVA_HOME%/bin
,您可以將 JAVA_HOME
動(dòng)態(tài)更改為任何版本您想要使用的 Java,而不必過(guò)多使用 PATH 變量.
Michael Borgwardt 在評(píng)論中提出了有趣的后續(xù)問(wèn)題
<塊引用>不過(guò),這并不能解釋為什么安裝程序在以前根本沒(méi)有設(shè)置 JAVA_HOME 時(shí)沒(méi)有設(shè)置它.
答案很簡(jiǎn)單:
設(shè)置無(wú)法知道腳本是否已經(jīng)依賴(lài)于JAVA_HOME
.
含義:一些腳本可以測(cè)試JAVA_HOME
的值,如果沒(méi)有設(shè)置,則引用另一個(gè)安裝在別處的JVM(不要忘記安裝"只能引用復(fù)制": JDK/JRE 并不總是由安裝程序安裝)
如果您設(shè)置 JAVA_HOME
,則可能會(huì)破壞某些腳本的默認(rèn)行為.
不想打擾那些依賴(lài)于未設(shè)置環(huán)境變量的假設(shè)腳本對(duì)我來(lái)說(shuō)聽(tīng)起來(lái)毫無(wú)意義的偏執(zhí) - 如果腳本這樣做,那么它顯然希望在安裝時(shí)使用不同的 JVM - 沒(méi)有理由避免這種情況.
嗯...甜蜜.對(duì)于每天處理大量部署問(wèn)題(用于我店的內(nèi)部應(yīng)用程序),我可以向您保證:這是非常理智的偏執(zhí)狂"對(duì)待擁有.
當(dāng)您部署到(非常)大量用戶(hù)時(shí),您不想對(duì)他們的平臺(tái)和配置做出任何假設(shè).顯然想要"是我不敢做出的假設(shè)(或者我將手機(jī)重定向到您的手機(jī);)并且您處理憤怒的電話(huà)).
例如,我們有許多腳本使用 sun 的 1.4.2 JVM(開(kāi)發(fā)??平臺(tái)上未設(shè)置 JAVA_HOME,直接在腳本中設(shè)置默認(rèn)路徑)或 JRockit 的 1.4.2(JAVA_HOME
集,因?yàn)樗羌伞㈩A(yù)生產(chǎn)和生產(chǎn)平臺(tái)的預(yù)期目標(biāo)).
但我們會(huì)定期安裝新的 JDK1.6.x,因?yàn)槲覀兪褂盟鼇?lái)啟動(dòng) eclipse.
假設(shè)那些腳本想要他們的 JAVA_HOME
設(shè)置...并且沒(méi)有任何工作了.
... Robert Grant 對(duì)此進(jìn)行了現(xiàn)場(chǎng)評(píng)論:
<塊引用><塊引用>您正在描述需要一個(gè)特定版本的腳本,但仍需查看全局 JAVA_HOME.這只是經(jīng)過(guò)深思熟慮的腳本.
雖然這可能是真的,也可能不是,但這也準(zhǔn)確地說(shuō)明了我的觀點(diǎn):
你不想做任何假設(shè)":不假設(shè)他們的平臺(tái)/設(shè)置,也不假設(shè)他們的最佳實(shí)踐".
前者可能聽(tīng)起來(lái)偏執(zhí),后者是普通常識(shí):認(rèn)為您的產(chǎn)品(這里是 JDK 設(shè)置)不會(huì)破壞用戶(hù)環(huán)境中的任何內(nèi)容,因?yàn)橛脩?hù)正確"地考慮了他的腳本......這太瘋狂了.
GvS 建議:
<塊引用>或者它可以選擇執(zhí)行此操作,默認(rèn)禁用
這意味著在設(shè)置屏幕中包含另一個(gè)選項(xiàng),該選項(xiàng)應(yīng)由用戶(hù)仔細(xì)查看,并且可能會(huì)產(chǎn)生意想不到的后果,即使用戶(hù)選擇它時(shí)認(rèn)為他知道自己在做什么......
這根本不值得.
I always wondered why I have to manually set the JAVA_HOME environment variable after installing the Java SDK.
JAVA_HOME=c:Program FilesJavajdk1.6.0_12
Visual Studio at least provides a batch file to set these kind of environment variables:
call "c:Program FilesMicrosoft Visual Studio 9.0VCvcvarsall.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.
You can install as many versions of Java as you like.
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.
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.
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.
Michael Borgwardt has made in the comments the interesting followup's question
Still, this does not explain why the installer does not set JAVA_HOME when it is not previously set at all.
The answer is simple:
The setup cannot know if a script already depends on JAVA_HOME
or not.
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)
If you set JAVA_HOME
, that can disrupt the default behavior of some of your scripts.
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.
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).
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).
But we install regularly the new JDK1.6.x since we use it for launching eclipse.
Assume that those scripts want their JAVA_HOME
set... and nothing works anymore.
... To which Robert Grant makes this on-the-spot critic:
You're describing scripts that require one specific version, but still look at global JAVA_HOME. That's just badly thought out scripts.
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.
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.
這篇關(guān)于為什么 Java SDK 安裝程序不設(shè)置 JAVA_HOME?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!