//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"));
}