Toby D
Toby D

Reputation: 1421

How can I call a main function of a Java class?

E.g: I got a Java class with the main function implemented like this:

public class Job{
    public static void main(String[] args) throws Exception{
        Job jobA = new Job();
        String jobName = System.getProperty("JobName");
        job.DoJobA("jobName");
    }

    public void DoJobA(String jobName){
        String configPath = System.getProperty("ConFig");
        File file = new File(configPath+ "/" + jobName);
        DoJobB(file);
    }
}

And I another class and want to call the main function of class Job but couldn't find a way to do that! Is there any advise for me?

Upvotes: 0

Views: 384

Answers (2)

RNJ
RNJ

Reputation: 15572

If you make the main method use var args then you dont need to pass in any variable

public static void main(String... args) throws Exception{
    ...
}

then you can just call it as

Job.main();

if you need arguments then you can pass them if.

Upvotes: 1

techfoobar
techfoobar

Reputation: 66693

You should be able to call it like you call any other static method

Job.main(yourArgs);

Upvotes: 8

Related Questions