java检查版本_如何检查Java版本
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 or JRE bin directory should be in the PATH variable, otherwise you will get an error as java command not found.
请注意, 或JRE bin⽬录应位于PATH变量中,否则将出现错误,因为java command not found 。
I have , and installed on my MacBook, so let’s see what is the result of running java version check command.
我在MacBook上安装了 , 和 ,所以让我们看看运⾏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 to get the java version details.
对于Java 8或更低版本,我们可以使⽤获取Java版本详细信息。
System.out.Property("java.version")); System.out.Property("java.specification.version"));
Output:
输出:
1.8.0_131
1.8
jdk怎么使用下载完整的程序代码
java检查版本
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论