DerBenniAusA
DerBenniAusA

Reputation: 807

Intellij: How to run all main() methods in a folder?

In IntelliJ there is a feature that runs all unit tests in a folder. Is there any possibility to run all main() methods in the same way?

Upvotes: 0

Views: 45

Answers (1)

Peter
Peter

Reputation: 5184

Not that I am aware of.

Workaround with wrapper class:

Create a class with a main method and call every main method in that method.

If these classes with main methods changes a lot, you could use this Reflection Library with the following code to scan for classes with a main method:

 Reflections reflections = new Reflections("your.package.with.main.classes");

 Set<Class<? extends Object>> allClasses = 
     reflections.getSubTypesOf(Object.class);

The allClasses set contains all classes in that package.

The following code would filter for classes that have a main method:

Set<Class> mainClasses = allClasses.stream()
  .filter(clazz -> 
    Arrays.stream(clazz.getMethods())
     .anyMatch(method -> 
        method.getName().equals("main")))
   .collect(Collectors.toSet());

Calling the main method should not be problem anymore.

PS: A filtering for static and public modifier would be a good idea as well.

Upvotes: 3

Related Questions