In a Java application I am developing I need to launch a method in a new Console: while running the application I want to invoke a method and execute its content in a new separate Console: is it possible?
Thanks in advance
In a Java application I am developing I need to launch a method in a new Console: while running the application I want to invoke a method and execute its content in a new separate Console: is it possible?
Thanks in advance
It's possible by creating a new process (java.lang.Process
) that takes care of executing the method. But because a process is only able to invoke main()
and cannot invoke seperate methods from some class, you need to wrap your desired method into another class:
public class SecondClass {
public static void main(String[] args) {
// if it's static
FirstClass.yourMethod(...);
// if it's non-static
new FirstClass().yourMethod(...);
}
}
Then by either using the ProcessBuilder class or Runtime.exec(), invoke SecondClass' main() method
Process p = new ProcessBuilder("java -cp [...] SecondClass").start();
or
Process p = Runtime.getRuntime().exec("[as above]");
you get the process instance. Put that code into FirstClass