Search code examples
javaprogram-entry-pointcompiler-warningscheckstylestatic-code-analysis

Warning: main methods should not be directly called


I currently work on a Java project which has many main methods. Now, I do some refactoring, which affects all main methods. However, some of the main methods are actually called directly from other methods. I think this is bad style and it actually makes the refactoring harder. Thus, I want to be able to identify all places where a main method is called. Currently, I use my IDE tools to find all references to a given main method, however this is tedious and it is easy to forget to check this for one of the main methods.

My question is, whether there is some checkstyle rule or Eclipse compiler warnings setting that produces a warning whenever a main method is called directly. I was unable to find one.


Solution

  • As @Jeutnarg suggested in the comments:

    [...] you could use Eclipse's Java search. Search for 'main', only searching for 'Method' with Limit to 'References' with Search in 'Sources' with a scope of your project.

    This is what I ended up doing and it actually worked much better than I expected.