I have the following code:
String methodName = "main";
Method[] methods = classHandle.getMethods();
for (Method m : methods)
{
System.out.println(m.getName().equals(methodName);
}
Method classMethod = null;
try
{
classMethod = classHandle.getMethod(methodName);
}
catch(Exception e)
{
}
System.out.println(classMethod == null);
The first print prints true, but the second one also prints true.
Why is that happening?
To get hold of static void main(String [] args) use the following
classHandle.getDeclaredMethod("main", String[].class);
Possible causes (written before we knew we were after static void main)
Consider the following.
private class Horse {
protected void makeNoise(int level) {
}
}
// OK
System.out.println(Horse.class.getDeclaredMethod("makeNoise", new Class<?>[]{int.class}));
// throws NoSuchMethodException - parameters don't match
System.out.println(Horse.class.getDeclaredMethod("makeNoise"));
// throws NoSuchMethodException, not a public method
System.out.println(Horse.class.getMethod("makeNoise", new Class<?>[]{int.class}));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With