I’m trying to get the list of all classes in a project in Java and I would like to identify the class where the main method is located. Is there a way to be able to identify that a class is implementing the main method, without actually looking at the code of the class itself?
I’ve implemented the following but the return value is always being false. does anyone know why this is happening?
Class<?> c = "edu.tool.parsing.A".getClass();
boolean hasMain = true;
try {
c.getMethod("main", String[].class);
hasMain=true;
} catch (SecurityException e) {
hasMain = true;
} catch (NoSuchMethodException e) {
hasMain=false;
}
Programmatically:
Class.getClass("com.mycompany.MyClass").getMethod("main", String[].class)Or alternatively you can use command line utility
javapthat you can find in your JDK bin directory.