2011-11-21 52 views
2

我開始用下面的參數的java:
-Xdebug -Xrunjdwp:transport=dt_socket,address=0,server=y,suspend=n
,我得到下面的輸出:
Listening for transport dt_socket at address: 59183我可以從JVM內部知道jdwp傳輸端口嗎?

是否有可能找到同一個JVM內部的端口,而不讀取標準輸出?

+0

https://stackoverflow.com/questions/28745065/identify-java-jdwp-debugger-assigned-ephemeral-port – shawn

回答

1

你爲什麼把端口設置爲0?通常,您可以使用地址參數將端口設置爲任何您想要的。

http://download.oracle.com/javase/1.4.2/docs/guide/jpda/conninv.html

+1

我想讓它變得自由港,所以它不會失敗。我將稍後連接到該端口。 – oshai

+0

@ohadshai - 它在哪裏被記錄爲使用端口0將導致它使用一個開放的端口? – david

+0

我不知道它是否記錄在案,但你可以在問題中看到結果。綁定端口時這是一種正常行爲。 – oshai

1
import lombok.extern.slf4j.Slf4j; 
import org.apache.commons.lang3.StringUtils; 

import java.lang.reflect.Method; 
import java.util.Properties; 

@Slf4j 
public class RuntimeDebugger { 

    static private int jdwpListenerPort; 

    static public int getJdwpListenerPort() { 
     if(jdwpListenerPort == 0) { 
      jdwpListenerPort = readJdwpListenerPort(); 
     } 
     return jdwpListenerPort; 
    } 

    static private int readJdwpListenerPort() { 
     String listenerAddress = null; 
     try { 
      Class<?> theClass = Class.forName("sun.misc.VMSupport"); 
      Method m = theClass.getMethod("getAgentProperties"); 
      Properties p = (Properties) m.invoke(null); 
      listenerAddress = p.getProperty("sun.jdwp.listenerAddress"); 
      if (listenerAddress != null) { 
       listenerAddress = StringUtils.substringAfter(listenerAddress, ":"); 
       return Integer.parseInt(listenerAddress); 
      } 
     } catch (Exception ex) { 
      log.error("Failed to read sun.jdwp.listenerAddress, ignore"); 
     } 
     return -1; 
    } 
} 
相關問題