admin管理员组文章数量:1123697
Let's say I want to get the content of java.runtime.version
to the Linux shell.
I don't want to call java -version
and or doing any kind of grepping / sedding as we used this approach already in the past and it proved to be flaky.
➜ java -version
openjdk version "21" 2023-09-19 LTS
OpenJDK Runtime Environment Temurin-21+35 (build 21+35-LTS)
OpenJDK 64-Bit Server VM Temurin-21+35 (build 21+35-LTS, mixed mode)
This e.g. example contains too much output which changes from release to release and also from vendor to vendor.
What I tried already was using jshell:
But the output still contains all the command feedback which I cannot disable (At least I don't know:
➜ printf $'System.out.println(java.lang.System.getProperty("java.runtime.version"));\n/exit\n' | jshell -s
-> System.out.println(java.lang.System.getProperty("java.runtime.version"))21+35-LTS
-> %
What I want in this example, is only the 21+35-LTS
part in a reliable and future proof way.
I am open to use tools available in an Ubuntu slim base image or in the recent Java jdk.
EDIT: I am not opposed to grep/sed in general as long as the solution is half-way future proof and works with different Java vendors.
Let's say I want to get the content of java.runtime.version
to the Linux shell.
I don't want to call java -version
and or doing any kind of grepping / sedding as we used this approach already in the past and it proved to be flaky.
➜ java -version
openjdk version "21" 2023-09-19 LTS
OpenJDK Runtime Environment Temurin-21+35 (build 21+35-LTS)
OpenJDK 64-Bit Server VM Temurin-21+35 (build 21+35-LTS, mixed mode)
This e.g. example contains too much output which changes from release to release and also from vendor to vendor.
What I tried already was using jshell:
But the output still contains all the command feedback which I cannot disable (At least I don't know:
➜ printf $'System.out.println(java.lang.System.getProperty("java.runtime.version"));\n/exit\n' | jshell -s
-> System.out.println(java.lang.System.getProperty("java.runtime.version"))21+35-LTS
-> %
What I want in this example, is only the 21+35-LTS
part in a reliable and future proof way.
I am open to use tools available in an Ubuntu slim base image or in the recent Java jdk.
EDIT: I am not opposed to grep/sed in general as long as the solution is half-way future proof and works with different Java vendors.
Share Improve this question edited 22 hours ago jaw asked yesterday jawjaw 1,0322 gold badges12 silver badges24 bronze badges 2 |3 Answers
Reset to default 5You can view all system properties with -XshowSettings, so something like this should work:
java -XshowSettings:properties 2>&1 | grep java.runtime.version
I don't think it's possible directly without some grepping/sedding of the output, but you can achieve it via an intermediate file, like this:
echo 'Files.writeString(Path.of("java_version"), Runtime.version().toString())' \
| jshell &>/dev/null \
&& cat java_version \
&& rm java_version
jshell &>/dev/null
will make sure jshell doesn't produce any output;Runtime.version()
is possibly a more straightforward alternative toSystem.getProperty("java.runtime.version")
.
You could try using --list-modules
. The way in which a module version is output seems to be more static (<module-name>@<version>
). Ask for the version of a module that implements the Java Language SE Specification and you get the version of the runtime (eg. java.base
).
java --list-modules --limit-modules java.base
outputs:
[email protected]
Add a cut
java --list-modules --limit-modules java.base | cut -d@ -f2
and you get:
21.0.5
NB. this version will not include any build information or vendor additions to the version. eg. On my system java.runtime.version
is 21.0.5+11-Ubuntu-1ubuntu122.04
This requires java 9 or above, and output format does not appear to have changed since java 9.
本文标签: bashHow to output the Java runtime version in one line to the shellStack Overflow
版权声明:本文标题:bash - How to output the Java runtime version in one line to the shell? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736586538a1945019.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
printf $'System.err.println(java.lang.System.getProperty("java.runtime.version"));\n/exit\n' | jshell -s >/dev/null
– user85421 Commented yesterday