I'm looking for a way to check which java version my software is running under.
I'd like to verify during load time my software is running on at least
-
2System.getProperty("java.version")NullPointerException– NullPointerException2013年06月26日 20:16:44 +00:00Commented Jun 26, 2013 at 20:16
-
Can you finish your last sentence.arshajii– arshajii2013年06月26日 20:21:45 +00:00Commented Jun 26, 2013 at 20:21
-
How many duplicate answers do we need?Steve Kuo– Steve Kuo2013年06月26日 20:59:37 +00:00Commented Jun 26, 2013 at 20:59
4 Answers 4
To get the java version you can use any of these depending on the version you want:
java.specification.version
java.version
java.vm.version
java.runtime.version
However, note that java versions are not equivalent between operative systems. So Java 6 on OSX does not mean the same thing as Java 6 on Windows. So, I would recommend you to also get the OS where the application is running, if you wish to determine if a given feature is available:
System.getProperty("os.name")
As a general guideline, all of this stuff is in the System package. A trick I use is iterate through all the available fields to have an idea of what I can use:
import java.util.Map;
class ShowProperties {
public static void main(String[] args) {
for (Map.Entry<Object, Object> e : System.getProperties().entrySet()) {
System.out.println(e);
}
}
}
Comments
Use java.version property to retrieve the jre version.
String javaVersion = System.getProperty("java.version");
if ((!javaVersion.startsWith("1.6")) && (!javaVersion.startsWith("1.7")) && (!javaVersion.startsWith("1.8")) && (!javaVersion.startsWith("1.9")))
{
// error
}
1 Comment
Comments
java.lang.System.getProperty("java.version")