I'm a newbie in JAVA programming and i'm trying to write simple MXBean servlet. I want to to dump some data from RuntimeMXBean.
As you know, there are couple methods for this interface, fe: - getVmName - getVmVendor and more..
I would like to create some array with couple method names and later use foreach to call one-by-one and print its value.
Unfortunately it does not work for me. I tried obj.invoke, getmethod and nothing - looks like it does not work like regular class or something (sorry, i'm noob)
Some code:
RuntimeMXBean rmx = ManagementFactory.getRuntimeMXBean();
If i use rmx.getVmVendor() all is fine, but i don't know how can i use (maybe something else?) invoke method with string name on this object (needed for loop) I'm using latest netbeans with glassfish.
Thank you.
You can use reflection API:
try {
Method method = rmx.getClass().getDeclaredMethod("aMethodName");
Object result = method.invoke(rmx);
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
System.out.println(ex);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.