I know I can get all the Java System properties from the terminal using
java -XshowSettings:properties -version
How do I access just one specific java system property?
For example, like "user.name"?
I want to do this in the terminal, not with Java.
Solution as a one liner script. Just change the val variable to the key you want to print:
val='java.library.path'; java -XshowSettings:properties -version 2>&1 | sed -re 's/^ +[^=]+ =/_&/' | gawk -v key=$val 'BEGIN{ RS="_"; IFS=" = "} { if($1 ~ key){ print $0 }}'
Details
Some property values like java.library.path contain new lines so we need to mark records before filtering and printing them.
sed allows us to do that, then awk can be used to filter and print.
java -XshowSettings:properties -version 2>&1 |\
sed -re 's/^ +[^=]+ =/_&/' |\
gawk -v key=java.library.path 'BEGIN{ RS="_"; IFS=" = "} { if($1 ~ key){ print $0 }}'
Result:
java.library.path = /usr/java/packages/lib/amd64
/usr/lib64
/lib64
/lib
/usr/lib
Pipeline parts explained:
2>&1: properties are printed to stderr so we need to redirect them to stdin.
sed -re 's/^ +[^=]+ =/_&/' : add an underscore in front of interesting lines, those starting with 4 spaces and containing =.
gawk -v key=java.library.path: set keyawk variable to the selected property key.
'BEGIN{ RS="_"; IFS=" = "}: set record separator to '_' and input field separator IFS to =.
If you need the current logged in user in bash just use whoami command. If you want to get the java property from terminal you can use the following command
java -XshowSettings:properties -version 2>&1 | grep user.name
which will print
$java -XshowSettings:properties -version 2>&1 | grep user.name
user.name = user
If you just need the user name only
java -XshowSettings:properties -version 2>&1 | grep user.name | cut -c 16-100
which will print
$java -XshowSettings:properties -version 2>&1 | grep user.name | cut -c 16-100
user
You can't.
What you can do is create a java file to get the information and run with java, here the documentation.
Since you already said that you don't want this, you can grep (filter) the output of
java -XshowSettings:properties -version 2>&1 | grep java.home
java.home = /usr/java/jdk1.8.0_112/jre
If you want to know the system properties of a running jvm, use the jcmd tool
jcmd PID VM.system_properties
Let's say I have a script to start some java stuff
start.sh
myStuff=10.0.0.13
port=11212
servers=(10.0.0.9 10.0.0.10 10.0.0.11 10.0.0.12 10.0.0.14)
T=$1
R=$2
nohup java -jar myJar.jar -l ${myStuff} -p ${port} -t $T -r $R -m "${servers[#]/%/:11212}" &
If I start the script manually
./start.sh 64 1
for example, everything works as it should
now
echo "nohup ./start.sh 64 1" | at now
however, does nothing.
Why?
I've started similar scripts like this before (none that called java, though) and I can't seem to figure out how they differ.
EDIT
well, not quite nothing, the job DOES get scheduled and does disappear from the atq, but the jar is not started and there is no nohup.out anywhere.
EDIT 2
note that
echo "nohup ./start.sh 64 1 > help &" | at now
does create a help file (not a nohup.out file, though) but it's empty.
EDIT 3
note that changing the start.sh such that the path to the java binary is hardcoded:
nohup /usr/lib/jvm/java-1.7.0-openjdk-amd64/bin/java -jar myJar.jar -l ${myStuff} -p ${port} -t $T -r $R -m "${servers[#]/%/:11212}" &
doesn't help either.
EDIT 4
echo "nohup java -version > help &" | at now
creates an empty help file (but no nohup.out).
Whereas
echo "nohup java -version > help 2>&1 &" | at now
will print
java version "1.7.0_111"
OpenJDK Runtime Environment (IcedTea 2.6.7) (7u111-2.6.7-0ubuntu0.14.04.3)
OpenJDK 64-Bit Server VM (build 24.111-b01, mixed mode)
into the help file (and not create a nohup.out file either).
EDIT 5
getting rid of the nohup, i.e.
java -jar myJar.jar ...
and then
echo "./start.sh 64 1" | at now
doesn't change anything.
How can I check whether Java is available (in the PATH or via JAVA_HOME) from a bash script and make sure the version is at least 1.5?
Perhaps something like:
if type -p java; then
echo found java executable in PATH
_java=java
elif [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]]; then
echo found java executable in JAVA_HOME
_java="$JAVA_HOME/bin/java"
else
echo "no java"
fi
if [[ "$_java" ]]; then
version=$("$_java" -version 2>&1 | awk -F '"' '/version/ {print $2}')
echo version "$version"
if [[ "$version" > "1.5" ]]; then
echo version is more than 1.5
else
echo version is less than 1.5
fi
fi
You can obtain java version via:
JAVA_VER=$(java -version 2>&1 | sed -n ';s/.* version "\(.*\)\.\(.*\)\..*".*/\1\2/p;')
it will give you 16 for java like 1.6.0_13, 15 for version like 1.5.0_17 and 110 for openjdk 11.0.6 2020-01-14 LTS.
So you can easily compare it in shell:
[ "$JAVA_VER" -ge 15 ] && echo "ok, java is 1.5 or newer" || echo "it's too old..."
UPDATE:
This code should work fine with openjdk and JAVA_TOOL_OPTIONS as mentioned in comments.
The answers above work correctly only for specific Java versions (usually for the ones before Java 9 or for the ones after Java 8.
I wrote a simple one liner that will return an integer for Java versions 6 through 11 (and possibly all future versions, until they change it again!).
It basically drops the "1." at the beginning of the version number, if it exists, and then considers only the first number before the next ".".
java -version 2>&1 | head -1 | cut -d'"' -f2 | sed '/^1\./s///' | cut -d'.' -f1
You can issue java -version and read & parse the output
java -version 2>&1 >/dev/null | grep 'java version' | awk '{print $3}'
I wrote a bash function that should work for JDK 9 and JDK 10.
#!/bin/bash
# returns the JDK version.
# 8 for 1.8.0_nn, 9 for 9-ea etc, and "no_java" for undetected
jdk_version() {
local result
local java_cmd
if [[ -n $(type -p java) ]]
then
java_cmd=java
elif [[ (-n "$JAVA_HOME") && (-x "$JAVA_HOME/bin/java") ]]
then
java_cmd="$JAVA_HOME/bin/java"
fi
local IFS=$'\n'
# remove \r for Cygwin
local lines=$("$java_cmd" -Xms32M -Xmx32M -version 2>&1 | tr '\r' '\n')
if [[ -z $java_cmd ]]
then
result=no_java
else
for line in $lines; do
if [[ (-z $result) && ($line = *"version \""*) ]]
then
local ver=$(echo $line | sed -e 's/.*version "\(.*\)"\(.*\)/\1/; 1q')
# on macOS, sed doesn't support '?'
if [[ $ver = "1."* ]]
then
result=$(echo $ver | sed -e 's/1\.\([0-9]*\)\(.*\)/\1/; 1q')
else
result=$(echo $ver | sed -e 's/\([0-9]*\)\(.*\)/\1/; 1q')
fi
fi
done
fi
echo "$result"
}
v="$(jdk_version)"
echo $v
This returns 8 for Java 8 ("1.8.0_151" etc), and 9 for Java 9 ("9-Debian" etc), which should make it easier to do the further comparison.
You could also compare the jdk class version number with javap:
javap -verbose java.lang.String | grep "major version" | cut -d " " -f5
If its java 8, this will print 52, and 55 for java 11. With this you can make a simple comparison and you don't have to deal with the version string parsing.
Determining the Java version only using grep with Perl-style regex can be done like this:
java -version 2>&1 | grep -oP 'version "?(1\.)?\K\d+'
This will print the major version for Java 9 or higher and the minor version for versions lower than 9, for example 8 for Java 1.8 and 11 for Java 11.0.4.
It can be used like this:
JAVA_MAJOR_VERSION=$(java -version 2>&1 | grep -oP 'version "?(1\.)?\K\d+' || true)
if [[ $JAVA_MAJOR_VERSION -lt 8 ]]; then
echo "Java 8 or higher is required!"
exit 1
fi
The || true was added to prevent the script from aborting in case Java is not installed and the Shell option set -e was enabled.
A combination of different answers:
JAVA_VER=$(java -version 2>&1 | grep -i version | sed 's/.*version ".*\.\(.*\)\..*"/\1/; 1q')
Returns 7 for Java 7 and 8 for Java 8
Works with OpenJDK and with Oracle JDK
Works even if the JAVA_TOOL_OPTIONS is set
in TERMINAL
java -version |& grep 'version' |& awk -F\" '{ split($2,a,"."); print a[1]"."a[2]}'
in BASH
#!/bin/bash
echo `java -version 2>&1 | grep 'version' 2>&1 | awk -F\" '{ split($2,a,"."); print a[1]"."a[2]}'`
jver=`java -version 2>&1 | grep 'version' 2>&1 | awk -F\" '{ split($2,a,"."); print a[1]"."a[2]}'`
if [[ $jver == "1.8" ]]; then
echo $jver is java 8
else
echo $jver is java 11
fi
Works in SUN and Linux
The method I ended up using is:
# Work out the JAVA version we are working with:
JAVA_VER_MAJOR=""
JAVA_VER_MINOR=""
JAVA_VER_BUILD=""
# Based on: http://stackoverflow.com/a/32026447
for token in $(java -version 2>&1 | grep -i version)
do
if [[ $token =~ \"([[:digit:]])\.([[:digit:]])\.(.*)\" ]]
then
JAVA_VER_MAJOR=${BASH_REMATCH[1]}
JAVA_VER_MINOR=${BASH_REMATCH[2]}
JAVA_VER_BUILD=${BASH_REMATCH[3]}
break
fi
done
It will work correctly even if JAVA_TOOL_OPTIONS is set to something due to filtering done by grep.
Another way is:
A file called release is located in $JAVA_HOME.
On Java 15 (Debian) it has as content:
IMPLEMENTOR="Debian"
JAVA_VERSION="15.0.1"
JAVA_VERSION_DATE="2020-10-20"
MODULES="java.base java.compiler java.datatransfer java.xml java.prefs java.desktop java.instrument java.logging java.management java.security.sasl java.naming java.rmi java.management.rmi java.net.http java.scripting java.security.jgss java.transaction.xa java.sql java.sql.rowset java.xml.crypto java.se java.smartcardio jdk.accessibility jdk.internal.vm.ci jdk.management jdk.unsupported jdk.internal.vm.compiler jdk.aot jdk.internal.jvmstat jdk.attach jdk.charsets jdk.compiler jdk.crypto.ec jdk.crypto.cryptoki jdk.dynalink jdk.internal.ed jdk.editpad jdk.hotspot.agent jdk.httpserver jdk.incubator.foreign jdk.internal.opt jdk.jdeps jdk.jlink jdk.incubator.jpackage jdk.internal.le jdk.internal.vm.compiler.management jdk.jartool jdk.javadoc jdk.jcmd jdk.management.agent jdk.jconsole jdk.jdwp.agent jdk.jdi jdk.jfr jdk.jshell jdk.jsobject jdk.jstatd jdk.localedata jdk.management.jfr jdk.naming.dns jdk.naming.rmi jdk.net jdk.nio.mapmode jdk.sctp jdk.security.auth jdk.security.jgss jdk.unsupported.desktop jdk.xml.dom jdk.zipfs"
OS_ARCH="x86_64"
OS_NAME="Linux"
SOURCE=""
So you see JAVA_VERSION, which contains the version number.
For example,
major=$(echo $JAVA_VERSION | cut -d. -f1)
sets major to the value 15 (=Major version number), which you can use further.
It's a really simple solution.
I had a similar problem, I wanted to check which version of java was installed to perform two types of application launches.
The following code has solved my problem
jdk_version() {
local result
local java_cmd
if [[ -n $(type -p java) ]]
then
java_cmd=java
elif [[ (-n "$JAVA_HOME") && (-x "$JAVA_HOME/bin/java") ]]
then
java_cmd="$JAVA_HOME/bin/java"
fi
local IFS=$'\n'
# remove \r for Cygwin
local lines=$("$java_cmd" -Xms32M -Xmx32M -version 2>&1 | tr '\r' '\n')
if [[ -z $java_cmd ]]
then
result=no_java
else
for line in $lines; do
if [[ (-z $result) && ($line = *"version \""*) ]]
then
local ver=$(echo $line | sed -e 's/.*version "\(.*\)"\(.*\)/\1/; 1q')
# on macOS, sed doesn't support '?'
if [[ $ver = "1."* ]]
then
result=$(echo $ver | sed -e 's/1\.\([0-9]*\)\(.*\)/\1/; 1q')
else
result=$(echo $ver | sed -e 's/\([0-9]*\)\(.*\)/\1/; 1q')
fi
fi
done
fi
echo "$result"
}
_java="$(jdk_version)"
echo $_java
if [[ "$_java" > "$8" ]]; then
echo version is more than 8
else
echo version is less than 8
fi
source
I hope to be proved helpful
There is a nice portable bash function for this here: http://eed3si9n.com/detecting-java-version-bash
jdk_version() {
local result
local java_cmd
if [[ -n $(type -p java) ]]
then
java_cmd=java
elif [[ (-n "$JAVA_HOME") && (-x "$JAVA_HOME/bin/java") ]]
then
java_cmd="$JAVA_HOME/bin/java"
fi
local IFS=$'\n'
# remove \r for Cygwin
local lines=$("$java_cmd" -Xms32M -Xmx32M -version 2>&1 | tr '\r' '\n')
if [[ -z $java_cmd ]]
then
result=no_java
else
for line in $lines; do
if [[ (-z $result) && ($line = *"version \""*) ]]
then
local ver=$(echo $line | sed -e 's/.*version "\(.*\)"\(.*\)/\1/; 1q')
# on macOS, sed doesn't support '?'
if [[ $ver = "1."* ]]
then
result=$(echo $ver | sed -e 's/1\.\([0-9]*\)\(.*\)/\1/; 1q')
else
result=$(echo $ver | sed -e 's/\([0-9]*\)\(.*\)/\1/; 1q')
fi
fi
done
fi
echo "$result"
}
Getting java version string, using Bash's built-in parsing capabilities:
#!/usr/bin/env bash
java_cmd=${1:-java}
# Captures version string between double-quotes
IFS='"' read -r _ java_version_string _ < <("${java_cmd}" -version 2>&1)
# Splits version string at dots and (underscore for patch number)
IFS='._' read -r \
java_version_major \
java_version_minor \
java_version_build \
java_version_patch \
<<<"$java_version_string"
# Debug demo output
declare -p java_version_major java_version_minor java_version_build \
java_version_patch
Examples
System default Java 17:
declare -- java_version_major="17"
declare -- java_version_minor="0"
declare -- java_version_build="3"
declare -- java_version_patch=""
Specific Java 8 path provided as argument:
declare -- java_version_major="1"
declare -- java_version_minor="8"
declare -- java_version_build="0"
declare -- java_version_patch="352"
I know this is a very old thread but this will still be of help to others.
To know whether you're running Java 5, 6 or 7, firstly type java -version.
There will be a line in your output that looks similar to this: java version "1.7.0_55"
Then you use this table for converting the 'jargon' result to the version number.
1.7.0_55 is Java 7
1.6.0_75 is Java 6
1.5.0_65 is Java 5
Information taken from a page on the Oracle site
http://www.oracle.com/technetwork/java/javase/7u55-relnotes-2177812.html
Using bashj, an extended bash version (https://sourceforge.net/projects/bashj/), you get a rather compact solution:
#!/usr/bin/bashj
echo System.getProperty("java.runtime.version")
The answer is provided by an integrated JVM (in ~0.017'' execution time for the script , and ~0.004'' for the call itself).
I found the following works rather well. It is a combination of the answers from Michał Šrajer and jmj
version=$(java -version 2>&1 | sed -n ';s/.* version "\(.*\)\.\(.*\)\..*"/\1\2/p;' | awk '{ print $1 }').
I have openJDK 13 installed and without the awk command the first answer printed: 130 2020-01-14. Thus, adding awk '{ print $1 }' provides me with just the version.
I am trying to get '6' out of the java version output given below
java version "1.6.0_21"
Java(TM) SE Runtime Environment (build 1.6.0_21-b07)
Java HotSpot(TM) Client VM (build 17.0-b17, mixed mode, sharing)
For the same I wrote this batch script
set VERSION6="1.6.0_21"
java -version 2>&1 | findstr "version" >ab.txt
for /f "tokens=3" %%g in (ab.txt) do (
if not %%g == %VERSION6% echo %%g
echo %%g
)
%%g displays "1.6.0_21"
May someone guide me to correct direction? I am not much familiar with for /f.
#echo off
setlocal
set VERSION6="1.6.0_21"
for /f "tokens=3" %%g in ('java -version 2^>^&1 ^| findstr /i "version"') do (
#echo Output: %%g
set JAVAVER=%%g
)
set JAVAVER=%JAVAVER:"=%
#echo Output: %JAVAVER%
for /f "delims=. tokens=1-3" %%v in ("%JAVAVER%") do (
#echo Major: %%v
#echo Minor: %%w
#echo Build: %%x
)
endlocal
In the first for loop, "tokens=3" says that we're going to just use the third token from the command output. Rather than redirect the output of the java -version command to a file, we can run this command within the for loop itself. The carets (^) are escape characters, and are needed so we can embed the >, & and | symbols in the command string.
Within the body of the for loop, we set a new var, JAVAVER, so that we can do some manipulation of the version string later.
The set JAVAVER=%JAVAVER:"=% command removes the double quotes from around the version string.
The last for loop parses the java version string. delims=. says we're going to delimit tokens using periods. tokens=1-3 says we're going to pass the first three tokens from the string to the body of the loop. We can now get the components of the java version string using the explicit variable, %%v and the implied variables (next letters in the alphabet) %%w and %%x.
When I run this on my system I get:
Output: "1.6.0_24"
Output: 1.6.0_24
Major: 1
Minor: 6
Build: 0_24
I've made some modification to Patrick's answer so it works with Java 9, 10, etc.
This returns minor version for 1.x, and major version for Java 9, 10, etc.
#echo off
setlocal
rem We use the value the JAVACMD environment variable, if defined
rem and then try JAVA_HOME
set "_JAVACMD=%JAVACMD%"
if "%_JAVACMD"=="" (
if not "%JAVA_HOME%"=="" (
if exist "%JAVA_HOME%\bin\java.exe" set "_JAVACMD=%JAVA_HOME%\bin\java.exe"
)
)
if "%_JAVACMD%"=="" set _JAVACMD=java
rem Parses x out of 1.x; for example 8 out of java version 1.8.0_xx
rem Otherwise, parses the major version; 9 out of java version 9-ea
set JAVA_VERSION=0
for /f "tokens=3" %%g in ('%_JAVACMD% -Xms32M -Xmx32M -version 2^>^&1 ^| findstr /i "version"') do (
set JAVA_VERSION=%%g
)
set JAVA_VERSION=%JAVA_VERSION:"=%
for /f "delims=.-_ tokens=1-2" %%v in ("%JAVA_VERSION%") do (
if /I "%%v" EQU "1" (
set JAVA_VERSION=%%w
) else (
set JAVA_VERSION=%%v
)
)
#echo %JAVA_VERSION%
endlocal
echoes 8 or 17 etc.
This will extract the minor part of the version number:
java -version 2>&1 | awk '/version/ {print $3}' | awk -F . '{print $2}'
However, it may be better to extract the major.minor and match on that in case Oracle ever change the version number scheme again e.g.:
java -version 2>&1 | awk '/version/ {print $3}' | egrep -o '[0-9]+\.[0-9]+'
for /f tokens^=2-5^ delims^=.-_+^" %j in ('java -fullversion 2^>^&1') do #set "jver=%j%k%l%m"
This will store the java version into jver variable and as integer
And you can use it for comparisons .E.G
if %jver% LSS 16000 echo not supported version
.You can use more major version by removing %k and %l and %m.This command prompt version.
For .bat use this:
#echo off
PATH %PATH%;%JAVA_HOME%\bin\
for /f tokens^=2-5^ delims^=.-_+^" %%j in ('java -fullversion 2^>^&1') do set "jver=%%j%%k%%l%%m"
According to my tests this is the fastest way to get the java version from bat (as it uses only internal commands and not external ones as FIND,FINDSTR and does not use GOTO which also can slow the script). Some JDK vendors does not support -fullversion switch or their implementation is not the same as this one provided by Oracle (better avoid them).
I'm trying to develop a bash build script for a Java project that will be run on Ubuntu and Fedora. Ubuntu uses the gcj compiler while Fedora uses IcedTea.
Both report their errors and warning in slightly different ways, and I want to ignore the warnings (I know, not generally a good idea, but some of the warnings are simply idiotic).
For gcj, I want to run:
javac *.java 2>&1 | grep -A 4 "error:"
but for IcedTea, I want to run:
javac *.java 2>&1 | grep -A 4 "error:\|errors\|.java:"
I'm still new to bash, so how would I write an if statement that would run one versus the other based upon the javac version?
Assuming your java and javac binaries match, and that icedtea is the special case.
#!/bin/bash
ERROR="error:"
java -version 2>&1 | grep -i icedtea > /dev/null
if [ $? -eq 0 ]; then
ERROR="error:\|errors\|.java:"
fi
javac *.java 2>&1 | grep -A 4 $ERROR
On my system, icedtea and sun have the same output for "javac -version", but not for "java -version".
Writing Java build scripts in bash (or any other shell language) has a number of problems:
scripts tend to be non-portable due to shell differences, different command locations, incompatible command options and so on ... even if you try to make the portable.
scripts cannot cope with dependencies (or at least not easily)
scripts cannot cope with recompiling only stuff that has changed
Instead, I suggest that you write a "build.xml" file and use the Ant build tool. Ant has the advantage of running on any build platform that runs Java, and of taking care of the vast majority of platform differences. It is sort of like a better "Make" designed specifically for building Java.
#!/bin/sh
JAVAC_VERSION="`java -version 2>&1 /dev/null | awk '/IcedTea/ {print $4}' | sed -e 's/[\(0-9]//g'`"
ICEDTEA="IcedTea"
if [ ${JAVAC_VERSION} = ${ICEDTEA} ]; then
javac *.java 2>&1 | grep -A 4 "error:\|errors\|.java:"
else
javac *.java 2>&1 | grep -A 4 "error:"
fi
exit 0
That should do it - if i understood your question correctly. How you get the version - im not quite sure of, but if my javac -version is incorrect just change it accordingly to your needs.