I have a program that requires dynamically (i.e. at runtime) opening an available socket and running the JMX agent on it. These JMX parameters are set inside Java code, not through the command line. It works great. After that, you need to control (for example, JMX commands, etc.) via Java Visual VM remotely
The RMI server agent in the program is in the management system outside the box described in: http://download.oracle.com/javase/6/docs/technotes/guides/management/agent.html
The question that I have can be summarized as: How can I set such command line properties to the system level through Java code so that I can use remote profiling
-Dcom.sun.management.jmxremote.port=1234
If "jmxremote.port" and other parameters are specified on the command line, remote monitoring works fine. I am trying to find a way to do this through Java and not through the command line.
The program cannot specify the port through the command line, because the new available port must be determined at runtime.
The process requires remote monitoring, and it works fine locally. If the following parameters are not specified on the command line, Java Visual VM does not connect to the process.
-Dcom.sun.management.jmxremote.port=1234 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=10.0.0.128
I tried.
System.setProperty("com.sun.management.jmxremote.port",Integer.toString(port));
This is one of the first things the program did before starting the JMXConnectorServer. Unfortunately, this is not recognized. Only run-time properties (i.e., those specified on the command line are recognized for JMX connections using Java Visual VM).
It also turned out that the properties can be extracted from the classes in the java collection, but could not find how to track the property "com.sun.management.jmxremote.port ="
public static void setEnv(Map<String, String> newenv) throws Exception { Class[] classes = Collections.class.getDeclaredClasses(); Map<String, String> env = System.getenv(); for(Class cl : classes) { if("java.util.Collections$UnmodifiableMap".equals(cl.getName())) { Field field = cl.getDeclaredField("m"); field.setAccessible(true); Object obj = field.get(env); Map<String, String> map = (Map<String, String>) obj;
Any help would be appreciated!