How can I extract the main classname of a java file ?
I am doing this in php!
I will read the whole java program as a string!

Please help!

<any_type/public/private/static> class **MainClass** {
    //Could be anything over here!
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

Recommended Answers

All 2 Replies

Member Avatar for LastMitch

@apanimesh061

How can I extract the main classname of a java file ?
I am doing this in php!

I don't think it can extract the main class of a java file.

Regex in PHP only can read and write text files or cvs files.

Java are in jar files. You can't extract a jar file by using PHP.

This should do the trick:

"(class)([\s\t ]+)(\w*)([\s\t\r\n ]*?)({+)"

$1 = class word
$2 = space between 'class' and the class name
$3 = class name
$4 = space between the class name and the '{'
$5 = the first '{' after the class name
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.