2013-01-09 45 views
1

我正在期待遠程確定通過JMX運行特定進程的Java版本。具體來說,我想要像「1.6.0_26」這樣的東西,這就是System.getProperty("java.version")會返回的東西。通過JMX確定Java版本

通過RunTime MBean,我可以檢查分別給出「Java HotSpot™64位服務器VM」和「20.1-b02」的VmName和VmVersion屬性。我不確定「20.1-b02」來自哪裏;有沒有辦法將它與「1.6.0_26」版本相匹配?

+1

@ HD1擁護?什麼?無論如何,我正在尋找使用JMX,而不是JDWP。 –

+0

VmVersion在我發佈的鏈接上有詳細記錄,@TylerHobbs – hd1

+0

也許在Java 7中改變了行爲?在Java 6中,正如我在我的問題中指出的那樣,它與「java.version」系統屬性不匹配。 –

回答

3

一種的java.lang.Runtime豆數量的提示:

  • specVersion要求
  • LibraryPath/BOOTCLASSPATH(最有可能在文件夾名稱)
  • SystemProperties - >java.runtime.version

編輯

由於@Kent指出代碼中的java.lang.management.RuntimeMXBean.getSystemProperties()與JConsole等JMX客戶端中的SystemProperties - >java.runtime.version相同。

+0

SpecVersion不幸的只是讀取「1.0」。我考慮過LibraryPath/BootClassPath,但正如你所說,這取決於具有非常特定命名要求的安裝目錄的路徑。我沒有意識到通過JMX檢查任何給定的SystemProperty的方法,但那會很棒。 –

+0

-1 SpecVersion不是他正在尋找的4.例如我的spec版本是1.7,但jre版本是「1.7.0_10」;我們不能/不應該依靠目錄名稱。我的JAVA_HOME是/ opt/java,LibraryPath基本上指向/ usr/lib,根本沒有版本號。並且OP正在通過SystemProperties尋找JMX.Not的路徑。 – Kent

+1

@Kent,這就是爲什麼我說「提示」,好嗎?而'SystemProperties'不是引用系統屬性,而是引用java.lang.Runtime bean的JMX'SystemProperties'屬性。它允許您瀏覽所有系統屬性。不要認爲你的倒票是適當的。 –

0

原來,老版本的JVM/JMX的返回錯誤的事情specVersion要求,所以使用@Kent提供更好的作品的技術:

 getLogger().finest("Checking JVM version"); 
    String specVersion = ManagementFactory.getRuntimeMXBean().getSpecVersion(); 
    getLogger().log(Level.FINEST, "Specification Version from JMX = ''{0}''", specVersion); 

    // Because of bug in the above call, earlier versions of JMX reported the version of 
    // JMX and not Java (so 1.6 reports 1.0 - replacing with the call below fixes this problem 
    // and consistently returns the specification of the JVM that is being used. 
    String javaVersion = ManagementFactory.getRuntimeMXBean().getSystemProperties().get("java.specification.version"); 
    getLogger().log(Level.FINEST, "java.specification.version found ''{0}''", javaVersion);