Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

current port used by the Java application

//Get the current port used by a Java application using socket programming in Java
private static int getPort() {
	    MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();
	    Set<ObjectName> objectNames = null;
	    try {
	        objectNames = beanServer.queryNames(new ObjectName("*:type=ProtocolHandler,*"),
	                Query.match(Query.attr("name"), Query.value(""https-*")));
	    } catch (MalformedObjectNameException e) {
	        log.error("SecuredPort not defined!", e);
	        return 0;
	    }

	    return Integer.parseInt(objectNames.iterator().next().getKeyProperty("port"));
	}
 
PREVIOUS NEXT
Tagged: #current #port #Java #application
ADD COMMENT
Topic
Name
9+5 =