java检查版本
Sometimes we need to check the Java version while executing a java program. We could be developing our project in local machine but deploying at a remote server, so we need to verify java version to make sure our program will work fine.
有时我们需要在执行Java程序时检查Java版本。 我们可能会在本地计算机上开发项目,但会在远程服务器上进行部署,因此我们需要验证Java版本以确保程序正常运行。
如何查看Java版本? (How to Check Java Version)
We can easily check the java version using the command line. We can use any of the following commands to print the java version.
我们可以使用命令行轻松地检查Java版本。 我们可以使用以下任何命令来打印Java版本。
$java -version
$java --version
java -version
will print version to the error stream and exit.
java -version
将打印版本到错误流并退出。
java --version
will print its version to the output stream and exit.
java --version
将其版本打印到输出流并退出。
Note that JDK or JRE bin directory should be in the PATH variable, otherwise you will get an error as java command not found
.
请注意, JDK或JRE bin目录应位于PATH变量中,否则将出现错误,因为java command not found
。
I have Java 8, Java 9 and Java 10 installed on my MacBook, so let’s see what is the result of running java version check command.
我在MacBook上安装了Java 8 , Java 9和Java 10 ,所以让我们看看运行Java version check命令的结果。
Java版本10输出 (Java Version 10 Output)
$ java -version
java version "10.0.1" 2018-04-17
Java(TM) SE Runtime Environment 18.3 (build 10.0.1+10)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.1+10, mixed mode)
$
Note that version is of the form $FEATURE.$INTERIM.$UPDATE.$PATCH
, so from the above output, we can deduce that Feature is 10, Interim is 0, Update is 1 and Patch is 0.
请注意,版本的形式$FEATURE.$INTERIM.$UPDATE.$PATCH
,因此从上面的输出中,我们可以推断出Feature为10,Interim为0,Update为1和Patch为0。
In the second line, what you see after + is called build number. So the build is 10. Note that this naming convention has come up in Java 10 itself. So it will be slightly different in older java versions.
在第二行中,在+之后看到的内容称为内部版本号。 因此构建为10。请注意,此命名约定已在Java 10本身中提出。 因此,在较早的Java版本中,它会稍有不同。
Java版本9输出 (Java Version 9 Output)
$ java -version
java version "9"
Java(TM) SE Runtime Environment (build 9+181)
Java HotSpot(TM) 64-Bit Server VM (build 9+181, mixed mode)
$
Java版本8输出 (Java Version 8 Output)
$ cd /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/bin/
$ ./java -version
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
$
Java版本11输出 (Java Version 11 Output)
$ java -version
openjdk version "11.0.1" 2018-10-16
OpenJDK Runtime Environment 18.9 (build 11.0.1+13)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.1+13, mixed mode)
$
我们可以通过编程方式检查Java版本吗? (Can we check java version programmatically)
What if we want to make sure our code is running on a compatible java version and maybe quit the application if it’s not the case. We can check the java version in the program also. Below code snippets show you how to check java version programmatically.
如果我们想确保我们的代码在兼容的Java版本上运行并且如果不是这种情况,则可能退出应用程序该怎么办? 我们也可以在程序中检查Java版本。 下面的代码片段向您展示了如何以编程方式检查Java版本。
Java 10的Java版本检查代码 (Java Version Check Code for Java 10)
Version version = java.lang.Runtime.version();
System.out.println("Java Version = "+version);
System.out.println("Java Version Feature Element = "+version.feature());
System.out.println("Java Version Interim Element = "+version.interim());
System.out.println("Java Patch Element Version = "+version.patch());
System.out.println("Java Update Element Version = "+version.update());
System.out.println("Java Version Build = "+version.build().get());
System.out.println("Java Pre-Release Info = "+version.pre().orElse("NA"));
Output:
输出:
Java Version = 10.0.1+10
Java Version Feature Element = 10
Java Version Interim Element = 0
Java Patch Element Version = 0
Java Update Element Version = 1
Java Version Build = 10
Java Pre-Release Info = NA
Note that the program output matches with the output from java -version
command.
请注意,程序输出与java -version
命令的输出匹配。
Java 9的Java版本检查代码 (Java Version Check Code for Java 9)
Version version = java.lang.Runtime.version();
System.out.println("Java Version = "+version);
System.out.println("Java Major Version = "+version.major());
System.out.println("Java Minor Version = "+version.minor());
System.out.println("Java Security Version = "+version.security());
System.out.println("Java Version Build = "+version.build().get());
System.out.println("Java Version Pre-Release Info = "+version.pre().orElse("NA"));
Version
class was first introduced in Java 9, in java 10 version major, minor and security properties were deprecated. If we execute above code snippet for Java 9, we get following output:
Version
类最初是在Java 9中引入的,在Java 10版本中,不推荐使用major,minor和security属性。 如果我们针对Java 9执行上述代码段,则会得到以下输出:
$ java JavaVersion
Java Version = 9+181
Java Major Version = 9
Java Minor Version = 0
Java Security Version = 0
Java Version Build = 181
Java Version Pre-Release Info = NA
Java 8或以下版本的Java版本检查代码 (Java Version Check Code for Java 8 or below versions)
For Java 8 or below, we can use System properties to get the java version details.
对于Java 8或更低版本,我们可以使用System属性获取Java版本详细信息。
System.out.println(System.getProperty("java.version"));
System.out.println(System.getProperty("java.specification.version"));
Output:
输出:
1.8.0_131
1.8
GitHub Repository GitHub存储库下载完整的程序代码
java检查版本