Is there any command-line function which will return the name of the main class from a huge Java file?

user11707156 :

I want to automate the process of downloading a huge number of Java files from the internet and test them one by one. However, most of the sites do not specify the name of the main class. Therefore, most of the time, I need to manually search for the string "public class" in order to determine the filename. Is there a better process to do it?

In general, the code format is:

import ...
import ...
import ...

class A {...}
class B {...}
public class C {...}
nullPointer :
grep -lP '\s*public\s+static\s+void\s+main\s*\(\s*(?:\[\s*\]\s*)?String\s*(?:\[\s*\])?\s*\w+\s*\)' *.java

The above command will return a list of .java files containing a main method, which is just an indication of being the Main class for each program.

As said above, a public class does not necessarily means it is the main class of a program, so the above grep command is your best chance in this case I believe.

Please also note that within the same app/program there could be more than one classes containing a main method, even though only one is actually used as the program's entry point.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=155653&siteId=1