下面列出了怎么用java.lang.Runtime.Version的API类实例代码及写法,或者点击链接到github查看源代码。
private static void test(String s, Integer major, Integer minor,
Integer sec, String pre, Integer build,
String opt)
{
Version v = testParse(s);
testStr(v.toString(), s);
testInt(v.major(), major);
testInt(v.minor(), minor);
testInt(v.security(), sec);
testStr((v.pre().isPresent() ? v.pre().get() : ""), pre);
testInt((v.build().isPresent() ? v.build().get() : 0), build);
testStr((v.optional().isPresent() ? v.optional().get() : ""), opt);
testVersion(v.version(), s);
}
public static void main(String[] args) {
printSystemProperty("java.version");
printSystemProperty("java.runtime.version");
printSystemProperty("java.vm.version");
printSystemProperty("java.specification.version");
printSystemProperty("java.vm.specification.version");
Version version = Runtime.version();
System.out.println();
System.out.println("Reported by runtime: " + version);
switch (version.major()) {
case 9:
System.out.println("Modularity!");
break;
case 10:
System.out.println("Value Types!");
break;
}
}
TaskFactory(JShell state) {
this.state = state;
this.compiler = ToolProvider.getSystemJavaCompiler();
if (compiler == null) {
throw new UnsupportedOperationException("Compiler not available, must be run with full JDK 9.");
}
Version current = Version.parse(System.getProperty("java.specification.version"));
if (INITIAL_SUPPORTED_VER.compareToIgnoreOptional(current) > 0) {
throw new UnsupportedOperationException("Wrong compiler, must be run with full JDK 9.");
}
this.fileManager = new MemoryFileManager(
compiler.getStandardFileManager(null, null, null), state);
}
JarFileSystem(ZipFileSystemProvider provider, Path zfpath, Map<String,?> env)
throws IOException {
super(provider, zfpath, env);
lookup = path -> path; // lookup needs to be set before isMultiReleaseJar is called
// because it eventually calls getEntry
if (isMultiReleaseJar()) {
int version;
Object o = env.get("multi-release");
if (o instanceof String) {
String s = (String)o;
if (s.equals("runtime")) {
version = Runtime.version().major();
} else {
version = Integer.parseInt(s);
}
} else if (o instanceof Integer) {
version = (Integer)o;
} else if (o instanceof Version) {
version = ((Version)o).major();
} else {
throw new IllegalArgumentException("env parameter must be String, Integer, "
+ "or Version");
}
lookup = createVersionedLinks(version < 0 ? 0 : version);
setReadOnly();
}
}
private static void tryCatch(String s, Class<? extends Throwable> ex) {
Throwable t = null;
try {
Version.parse(s);
} catch (Throwable x) {
if (ex.isAssignableFrom(x.getClass())) {
t = x;
} else
x.printStackTrace();
}
if ((t == null) && (ex != null))
fail(s, ex);
else
pass();
}
private static void testVersion() {
Version current = Runtime.version();
String javaVer = System.getProperty("java.runtime.version");
// java.runtime.version == $VNUM(\-$PRE)?(\+$BUILD)?(-$OPT)?
String [] jv = javaVer.split("\\+");
String [] ver = jv[0].split("-");
List<Integer> javaVerVNum
= Arrays.stream(ver[0].split("\\."))
.map(Integer::parseInt)
.collect(Collectors.toList());
if (!javaVerVNum.equals(current.version())) {
fail("Runtime.version()", javaVerVNum.toString(),
current.version().toString());
} else {
pass();
}
Optional<String> javaVerPre
= (ver.length == 2)
? Optional.ofNullable(ver[1])
: Optional.empty();
if (!javaVerPre.equals(current.pre())) {
fail("testCurrent() pre()", javaVerPre.toString(),
current.pre().toString());
} else {
pass();
}
testEHC(current.toString(), javaVer, true, true, 0, 0);
}
private static void testEHC(String s0, String s1, boolean eq, boolean eqNO,
int cmp, int cmpNO)
{
Version v0 = Version.parse(s0);
Version v1 = Version.parse(s1);
testEquals(v0, v1, eq);
testEqualsNO(v0, v1, eqNO);
testHashCode(v0, v1, eq);
testCompare(v0, v1, cmp);
testCompareNO(v0, v1, cmpNO);
}
private static void testEqualsNO(Version v0, Version v1, boolean eq) {
if (eq == v0.equalsIgnoreOptional(v1)) {
pass();
} else {
fail("equalsIgnoreOptional() " + Boolean.toString(eq),
v0.toString(), v1.toString());
}
}
private static void testEquals(Version v0, Version v1, boolean eq) {
if (eq == v0.equals(v1)) {
pass();
} else {
fail("equals() " + Boolean.toString(eq),
v0.toString(), v1.toString());
}
}
private static void testHashCode(Version v0, Version v1, boolean eq) {
int h0 = v0.hashCode();
int h1 = v1.hashCode();
if (eq) {
testInt(h0, h1);
} else if (h0 == h1) {
fail(String.format("hashCode() %s", h0),
Integer.toString(h0),
Integer.toString(h1));
} else { // !eq && (h0 != h1)
pass();
}
}
private static void checkCompare(Version v0, Version v1,
int expected, int actual)
{
if (Integer.signum(expected) == Integer.signum(actual)) {
pass();
} else {
fail(String.format("compare() (actual = %s) (expected = %s)",
actual, expected),
v0.toString(), v1.toString());
}
}
@DataProvider(name="versions")
public Object[][] createVersions() {
return new Object[][] {
{Version.parse("8"), 8},
{Version.parse("9"), 9},
{Version.parse("10"), 10},
{Version.parse("11"), 10},
{Version.parse("100"), 10}
};
}
public static void main(String[] args) {
Version version = Runtime.version();
// these were all deprecated in Java 10
System.out.println(version.major() + "." + version.minor() + "." + version.security());
// use these instead
System.out.println(version.feature() + "." + version.interim() + "." + version.update() + "." + version.patch());
}
public static void main(String[] args) {
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"));
}
public static void main(String[] args) {
// Java 10 Onwards
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"));
/** Version class introduced in 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"));
*/
// For Java 8 or lower, use System Property
//System.out.println(System.getProperty("java.version"));
//System.out.println(System.getProperty("java.specification.version"));
}
private static Version testParse(String s) {
Version v = Version.parse(s);
pass();
return v;
}
private static void testCompareNO(Version v0, Version v1, int compare) {
int cmp = v0.compareToIgnoreOptional(v1);
checkCompare(v0, v1, compare, cmp);
}
private static void testCompare(Version v0, Version v1, int compare) {
int cmp = v0.compareTo(v1);
checkCompare(v0, v1, compare, cmp);
}
@Test(dataProvider="versions")
public void testVersions(Version value, int expected) throws Throwable {
versionEnv.put("multi-release", value);
runTest(versionEnv, expected);
}
public static void main(String[] args) {
Version version = Runtime.version();
// these were all deprecated in Java 10
System.out.println(version.major() + "." + version.minor() + "." + version.security());
}