[英]Intellij: How to run all main() methods in a folder?
In IntelliJ there is a feature that runs all unit tests in a folder. 在IntelliJ中,有一项功能可以运行文件夹中的所有单元测试。 Is there any possibility to run all main() methods in the same way? 是否有可能以相同的方式运行所有main()方法?
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. 用main
方法创建一个类,并调用该方法中的每个main
方法。
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: 如果这些具有main
方法的类发生了很大变化,则可以将此反射库与以下代码一起使用,以扫描具有main
方法的类:
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. allClasses
集包含该软件包中的所有类。
The following code would filter for classes that have a main
method: 以下代码将筛选具有main
方法的类:
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. 调用main
方法应该不再是问题。
PS: A filtering for static
and public
modifier would be a good idea as well. PS:对static
和public
修饰符进行过滤也是一个好主意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.