Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
287 views
in Technique[技术] by (71.8m points)

Can I invoke a java method other than main(String[]) from the command line?

Can I invoke a java method other than main(String[]) from the command line?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you don't have a main function, you can just add one, and if you do, you can just add a series of if-then blocks to the top.

public static void main(String[] args){
    if (args[0].equals("MY_METHOD"))
        callMyMethod();
    else if(args[0].equals("MY_OTHER_METHOD"))
        callMyOtherMethod();
    //... Repeat ad nauseum...
    else {
        //Do other main stuff, or print error message
    }
}

Then, from the command line:

$ java [MyPackage.]MyClass MY_METHOD

Will run your method.

This is pretty hackish - I'm almost sure it's not what you want to do, but hey, it answers the question, right?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...