Come trovare la versione JVM da un programma?
Voglio scrivere un file Java di esempio in cui voglio conoscere la versione JVM in cui è in esecuzione la classe. C'è un modo?
10 answers
System.getProperty("java.version")
restituisce quello che ti serve.
Puoi anche usare JMX se vuoi:
ManagementFactory.getRuntimeMXBean().getVmVersion()
Sembra che java.specification.version
sia il migliore per il lavoro.
Ad esempio da un'applet in uno dei miei siti precedenti:
java.specification.version 1.6
java.version 1.6.0_23
java.vm.version 19.0-b09
java.runtime.version unknown
Il valore unknown
per java.runtime.version
significa semplicemente che l'applet non è attendibile. Qui è nella versione attendibile:
java.specification.version 1.6
java.version 1.6.0_23
java.vm.version 19.0-b09
java.runtime.version 1.6.0_23-b05
System.getProperty("java.version")
Ecco un elenco delle proprietà di sistema standard, tra cui:
-
java.vm.specification.version
Versione specifica della macchina virtuale Java -
java.vm.specification.vendor
Fornitore delle specifiche della macchina virtuale Java -
java.vm.specification.name
Nome della specifica della macchina virtuale Java -
java.vm.version
Versione di implementazione della macchina virtuale Java -
java.vm.vendor
Fornitore di implementazione della macchina virtuale Java -
java.vm.name
Nome implementazione Java Virtual Machine -
java.specification.version
Java Runtime Environment specification version -
java.specification.vendor
Fornitore di specifiche Java Runtime Environment -
java.specification.name
Nome specifica Java Runtime Environment
Semplicemente un caso di chiamata System.getProperty("java.version")
.
Esempi di utilizzo java.version
, java.specification.version
e java.runtime.version
: Ottenere la versione di java in runtime .
Sotto il codice java restituisce le versioni JVM
disponibili nell'IDE corrente
List<VirtualMachineDescriptor> descriptors = VirtualMachine.list();
for (VirtualMachineDescriptor descriptor : descriptors) {
System.out.println("Found JVM: " + descriptor.displayName());
try {
VirtualMachine vm = VirtualMachine.attach(descriptor);
String version = vm.getSystemProperties().getProperty("java.runtime.version");
System.out.println(" Runtime Version: " + version);
String connectorAddress = vm.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress");
if (connectorAddress == null) {
connectorAddress = vm.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress");
}
JMXServiceURL url = new JMXServiceURL(connectorAddress);
JMXConnector connector = JMXConnectorFactory.connect(url);
MBeanServerConnection mbs = connector.getMBeanServerConnection();
ObjectName threadName = new ObjectName(ManagementFactory.THREAD_MXBEAN_NAME);
Integer threadCount = (Integer)mbs.getAttribute(threadName, "ThreadCount");
System.out.println(" Thread count: " + threadCount);
}
catch (Exception e) {
// ...
}
Uscita:
Found JVM: /home/.../STS IDE/sts-bundle/sts-3.7.2.RELEASE//plugins/org.eclipse.equinox.launcher_1.3.100.v20150511-1540.jar -os linux -ws gtk -arch x86_64 -showsplash -launcher /home/.../STS IDE/sts-bundle/sts-3.7.2.RELEASE/STS -name STS --launcher.library /home/.../STS IDE/sts-bundle/sts-3.7.2.RELEASE//plugins/org.eclipse.equinox.launcher.gtk.linux.x86_64_1.1.300.v20150602-1417/eclipse_1612.so -startup /home/.../STS IDE/sts-bundle/sts-3.7.2.RELEASE//plugins/org.eclipse.equinox.launcher_1.3.100.v20150511-1540.jar --launcher.overrideVmargs -exitdata 1ad000f -product org.springsource.sts.ide -vm /usr/bin/java -vmargs -Dosgi.requiredJavaVersion=1.7 -Xms40m -XX:MaxPermSize=256m -Xverify:none -Xmx1200m -jar /home/.../STS IDE/sts-bundle/sts-3.7.2.RELEASE//plugins/org.eclipse.equinox.launcher_1.3.100.v20150511-1540.jar
Runtime Version: 1.8.0_91-b14
Found JVM: com.intellij.idea.Main
Runtime Version: 1.8.0_91-b14
Found JVM: Test
Runtime Version: 1.7.0_80-b15
A seconda di ciò di cui uno ha bisogno, le altre risposte possono aiutare.
Nel mio caso, non l'hanno fatto. Stavo cercando le informazioni sulla versione "completamente qualificate" di un IBM JDK.
Quindi, la risposta "reale" può essere: basta scaricare tutte le proprietà di sistema e verificare se ce n'è una che ti dà quello che stai cercando.
Nel mio caso; ho scoperto che l'IBM JDK conosce un
Proprietà: java.fullversion
Per ulteriori informazioni, consultare il sito:Riferimenti 20161013_322271 (JIT abilitato, AOT abilitato) J9VM-R28_Java8_SR3_20161013_1635_B322271
JIT-tr.r14.Java.green_20161011_125790
GC-R28_Java8_SR3_20161013_1635_B322271_CMPRSS J9CL - 20161013_322271
Le informazioni sulle versioni vengono memorizzate come proprietà della classe System
.
Http://docs.oracle.com/javase/6/docs/api/java/lang/System.html#getProperties%28%29
Sistema.getProperty ("sole.arco.dati.modello");
Controllo Java a 32 bit e 64 bit
Integer vers = Integer.parseInt(System.getProperty("java.version").split("\\.")[1]);
String bitMode = System.getProperty("sun.arch.data.model").toString();
System.out.println(vers);
System.out.println(bitMode);
Uscita:
6
32
Basta semplicemente chiamare,
System.out.println(System.getProperty("java.specification.version"));
System.out.println(System.getProperty("java.runtime.version"));
Esempio di output:
9
9+176