What is the good way of setting JAVA_HOME system wide in Linux? /etc/profile or /etc/profile.d/custom.sh?

笑着哭i 提交于 2019-12-06 23:27:33

问题


There doesn't seem to be an official answer as to where to set your system wide JAVA_HOME in Linux, at least I haven't found one (looked on Oracle website and some google fu). Many forums and comments point at setting it in /etc/profile or even /etc/bashrc/ (or /etc/bashrc.bashrc) but on my system both these files headers are quite specific about creating

"a custom.sh shell script in /etc/profile.d/ [...] as this will prevent the need for merging in future updates."

instead of directly modifying the files.

For this reason I'm thinking the custom.sh shell script placed in /etc/profile.d is the way to go but I might be missing something hence this question =)

Please forgive me if I missed an official doc and just post the link!



回答1:


You pretty much answered your own question with the paragraph beginning "For this reason...". The only time I edit /etc/profile is when I'm changing some value in it (rather than adding something).

When adding various software packages which require changing user profiles, most distros add an appropriate script to the /etc/profile.d directory, and the base-installed /etc/profile script has a call to all executable scripts in that directory. (Likewise, upgrading/removing the distro-provided package will have the script in the subdirectory modified or removed.)




回答2:


I usually make a java_dev.sh file in /etc/profile.d/ containing:

export JAVA_HOME=/opt/javahome
export M2_HOME=/opt/mavenhome
export PATH=${JAVA_HOME}/bin:${M2_HOME}/bin:${PATH}

Or the like. I use this on all Linuxen I frequently use, Ubuntu, RH, Fedora...



来源:https://stackoverflow.com/questions/10117943/what-is-the-good-way-of-setting-java-home-system-wide-in-linux-etc-profile-or

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!