在CentOS Box上正确指定JAVA_HOME

时间:2021-07-08 11:48:23

Using Vagrant and Chef on a CentOS 6.5 box, I have the following cookbooks:

在CentOS 6.5盒子上使用Vagrant和Chef,我有以下食谱:

chef.add_recipe "yum" 
chef.add_recipe "custom_java_cookbook" # simply installs the Oracle Java 7 RPM
chef.add_recipe "set_env_var"

Overall, my problem is how to correctly specify JAVA_HOME to use the Oracle Java 7 version over the yum-installed OpenJDK6.

总的来说,我的问题是如何正确指定JAVA_HOME以使用Oracle Java 7版本而不是yum安装的OpenJDK6。

set_env_var sets the following environment variables:

set_env_var设置以下环境变量:

  • M2_HOME
  • GRADLE_HOME
  • JAVA_HOME

Using the magic shell cookbook, I successfully set the first 2 environment variables, but not JAVA_HOME. However, after running vagrant up --provision, my $JAVA_HOME is set to the open-jdk6 directory.

使用magic shell cookbook,我成功设置了前2个环境变量,但没有成功设置JAVA_HOME。但是,在运行vagrant -provision之后,我的$ JAVA_HOME被设置为open-jdk6目录。

Looking in /etc/profile.d, I have 2 shell scripts corresponding to setting JAVA_HOME:

查看/etc/profile.d,我有2个shell脚本对应于设置JAVA_HOME:

-jdk.sh # I'm guessing it's part of the yum cookbook. {export JAVA_HOME = openjdk6}
-JAVA_HOME.sh # Chef-created script: {export JAVA_HOME = jdk7_oracle_dir}

How can I specify which JAVA_HOME environment script to use under /etc/profile.d?

如何指定在/etc/profile.d下使用哪个JAVA_HOME环境脚本?

It seems that the jdk.sh is winning over the JAVA_HOME.sh since my JAVA_HOME is set to the openjdk6 version.

似乎jdk.sh赢得了JAVA_HOME.sh,因为我的JAVA_HOME被设置为openjdk6版本。

1 个解决方案

#1


1  

If you are going to set JAVA_HOME yourself via a custom cookbook/recipe, you should probably remove the existing jdk.sh in the same recipe. Something like:

如果您要通过自定义菜谱/食谱自己设置JAVA_HOME,您应该删除相同配方中的现有jdk.sh。就像是:

file "/etc/profile.d/jdk.sh" do
  action :delete
end

Would be useful to wrap that in checks to only apply when absolutely necessary (i.e., if file exists, if yum installed, if platform is what you expect, etc)

将它包装在支票中以便仅在绝对必要时应用(例如,如果文件存在,如果安装了yum,如果平台是您期望的等等)将是有用的

#1


1  

If you are going to set JAVA_HOME yourself via a custom cookbook/recipe, you should probably remove the existing jdk.sh in the same recipe. Something like:

如果您要通过自定义菜谱/食谱自己设置JAVA_HOME,您应该删除相同配方中的现有jdk.sh。就像是:

file "/etc/profile.d/jdk.sh" do
  action :delete
end

Would be useful to wrap that in checks to only apply when absolutely necessary (i.e., if file exists, if yum installed, if platform is what you expect, etc)

将它包装在支票中以便仅在绝对必要时应用(例如,如果文件存在,如果安装了yum,如果平台是您期望的等等)将是有用的