In Java, can we call a main() method from another class? For example, ClassA is meant to be run on the command line as it has a main(String[] args) method defined.
I want to call this ClassA inside ClassB. Do I initialize a new object of the class, like
ClassA newobject = new ClassA()
or is there a way to pass the command line options of ClassA inside ClassB.
thanks
A.main(...string args here);
That's all there is to it because it's just a simple static void method. Fair bit of warning, it will be run in the same process and probably thread too as the caller. If what you are looking for is to have it run it as a real, honest to goodness different program then you'll need to use the Java Process API to actually spawn a proper new JVM. You can probably get away with this for a lot of things small programs:
Runnable r = new Runnable() {
public void run() {
A.main(argsArray);
}
};
Thread t = new Thread(r);
t.start();
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments