简体   繁体   English

有没有办法检查一个类是否具有main方法?

[英]is there a way to check if a class has the main method?

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. 我正在尝试获取Java项目中所有类的列表,我想确定main方法所在的类。 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? 有没有一种方法可以识别类是否正在实现main方法,而无需实际查看类本身的代码?

I've implemented the following but the return value is always being false. 我实现了以下内容,但返回值始终为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 javap that you can find in your JDK bin directory. 或者,您可以使用在JDK bin目录中找到的命令行实用程序javap

If you have the class name, then you can try to reflect the main method. 如果您具有类名,则可以尝试反映main方法。

Trivial (inclomplete) approach: 普通(渐进式)方法:

private static hasMainMethod(Class<?> clazz) throws Exception {
  Method[] methods = clazz.getMethods();
  for (Mehthod method:methods) {
    if (method.getName().equals("main") {
      // Now we have to verify the method signature!
      return true;
    }
  }
  return false;
}

There may be more than one such class. 这样的类可能不止一个。 There may be dozens. 可能有几十个。 Why don't you know the entry point in advance? 您为什么不提前知道入口点? You might be better off looking at the Main-Class entry in the JAR Manifest. 您最好查看JAR清单中的Main-Class条目。

Load your project in a IDE (I have worked with IDEA) then, add a local run, IDEA will list you all classes with main method. 然后将您的项目加载到IDE中(我曾与IDEA一起工作过),然后添加本地运行,IDEA将列出您所有带有main方法的类。

If you don't have sources and there are just jar files, that's ok, just add the jar files as a library of a project and then create a RUN. 如果您没有源并且只有jar文件,那么可以,只需将jar文件添加为项目的库,然后创建RUN。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有办法检查一个类是否有一个方法,然后在已经存在的对象实例上调用它? - Is there a way to check if a class has a method and then invoke it on an already existing instance of the object? 是否可以从已经具有main方法的类中调用main方法? - Is it possible to call to main method from a class that already has a main method? 如何检查java类中是否有特定的方法? - How to check if a java class has a particular method in it? Run a CHILD in Eclipse of which the PARENT class has the main method but the CHILD class itself is just a class without the main method - Run a CHILD in Eclipse of which the PARENT class has the main method but the CHILD class itself is just a class without the main method 有没有办法检测一个方法是否已被删除或一个类是否已被删除? 爪哇 - Is there any way of detecting if a method has been removed or a class has? Java 如果对象具有特定方法,是否有办法检查Java运行时? - Is there a way to check in Java runtime if an object has a specific method? 类 main 的方法未定义 - method is undefined for class main main()是一个类方法吗? (JAVA) - Is main() a class method? (Java) 在课堂上找不到主要方法 - Main method not found in class 如何检查所需的类是否具有必需的方法 - How to check whether the needed class has required method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM