简体   繁体   English

在bash脚本中包含java -version命令

[英]Include the java -version command in bash script

[Message updated to include script and its output as you asked]. [根据您的要求,消息已更新为包括脚本及其输出]。

I have created a linux script which installs Oracle Java on Ubuntu. 我创建了一个Linux脚本,该脚本在Ubuntu上安装Oracle Java。

The script is the following: 脚本如下:

#!/bin/bash
# This script installs Oracle Java 7u51 jdk on Ubuntu Linux 64 bit

echo "Starting Oracle Java JDK Installation..."
sudo mkdir /usr/local/java
cd
sudo cp -r jdk-7u51-linux-x64.tar.gz /usr/local/java
rm jdk-7u51-linux-x64.tar.gz
cd /usr/local/java

echo "Unpacking java files in /usr/local/java"
sudo tar xvzf jdk-7u51-linux-x64.tar.gz
sudo rm jdk-7u51-linux-x64.tar.gz
sleep 1s

echo "Updating system Path file /etc/profile with Java variables"
sudo sed -i '$ a\JAVA_HOME=/usr/local/java/jdk1.7.0_51' /etc/profile
sudo sed -i '$ a\PATH=$PATH:$HOME/bin:$JAVA_HOME/bin' /etc/profile
sudo sed -i '$ a\export JAVA_HOME' /etc/profile
sudo sed -i '$ a\export PATH' /etc/profile
sleep 1s

echo "Updating alternatives"
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/local/java/
jdk1.7.0_51/bin/java" 1
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/local/java/
jdk1.7.0_51/bin/javac" 1
sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/local/java/
jdk1.7.0_51/bin/javaws" 1
sudo update-alternatives --set java /usr/local/java/jdk1.7.0_51/bin/java
sudo update-alternatives --set javac /usr/local/java/jdk1.7.0_51/bin/javac
sudo update-alternatives --set javaws /usr/local/java/jdk1.7.0_51/bin/javaws
sleep 3s

echo "Reload system wide Path /etc/profile"
. /etc/profile
sleep 3s

echo "Testing if Java is installed correctly. System must reply with Java version."
java –version
sleep 1s
javac -version

The script runs fine doing its thing and Java is installed correctly. 该脚本可以很好地运行,并且Java已正确安装。 But the java -version command at the end gives this output at the terminal. 但是最后的java -version命令会在终端上提供此输出。

Testing if Java is installed correctly. 测试Java是否正确安装。 System must reply with Java version. 系统必须回复Java版本。

Error: Could not find or load main class –version 错误:找不到或加载主类–版本

javac 1.7.0_51 javac 1.7.0_51

Two things baffle me. 两件事让我感到困惑。 The first is that the javac -version commands works in the script but not the java -version command. 首先是javac -version命令可在脚本中使用,而java -version命令则不可。

The second is that if I go to the terminal right after the script has ended and input myself java -version, it works. 第二个是,如果我在脚本结束后立即进入终端并输入我自己的java -version,那么它将起作用。

The install may be adding to the PATH, which you won't see from inside the current shell. 该安装可能会添加到PATH,您将无法从当前shell内看到该安装。 Try it with 尝试一下

sh -c java -version

也许您可以更改调用顺序,也可以在更新替代操作之前调用重装系统范围的Path操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM