0

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

  • 1
    No. A new console requires a new process, which means it requires a new program, not just a new method. – Andreas Jun 14 '16 at 21:54
  • So what kind of solution do you suggest? At the moment the method is actually a class, potentially a different program... My goal is to launch the new program + new console from the first with something like a method invocation... I don't want the user to launch the 2 programs manually – Mark Driver Jun 14 '16 at 22:01
  • Put the other code in a new jar so you can run it using `java -jar newjar.jar`, then run it as described here, depending on your OS: [Open a new prompt/terminal window from Java](http://stackoverflow.com/q/5738259/5221149) – Andreas Jun 14 '16 at 22:04
  • I am trying these two ways but it does not work: a new Terminal is opened but no commands are executed and the jar is not opened... `try { Runtime.getRuntime().exec("/usr/bin/open -a Terminal /Users/.../src/temp.jar"); } catch (IOException e) { e.printStackTrace(); } / / String[] argS = new String[] {"/bin/bash", "-c", "java", "-jar", "/Users/.../src/temp.jar"}; try { Process proc = new ProcessBuilder(argS).start(); } catch (IOException e) { e.printStackTrace(); }` – Mark Driver Jun 15 '16 at 08:17

1 Answers1

0

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

bcsb1001
  • 2,834
  • 3
  • 24
  • 35
Wecherowski
  • 818
  • 11
  • 24
  • The method I want to execute actually is the main of a new class that I have exported in Jar format. I am trying these two ways but it does not work: a new Terminal is opened but no commands are executed and the jar is not opened...`try { Runtime.getRuntime().exec("/usr/bin/open -a Terminal /Users/.../src/temp.jar"); } catch (IOException e) { e.printStackTrace(); } / / String[] argS = new String[] {"/bin/bash", "-c", "java", "-jar", "/Users/.../src/temp.jar"};` `try { Process proc = new ProcessBuilder(argS).start(); } catch (IOException e) { e.printStackTrace(); }` – Mark Driver Jun 15 '16 at 09:09
  • Are you using Linux? The prompt you found there is for UNIX systems. You need to change the delimiters from "/" to "\". You enter basically the same command you would use to invoke the jar. Usually it's "java -jar -cp[classpath] JAR_NAME.jar" – Wecherowski Jun 15 '16 at 19:58
  • @PhilEvening `-jar` and `-cp` are mutually exclusive. The `MANIFEST.MF` file controls the classpath when you give `-jar`, so `-cp` or the `CLASS_PATH` environment variable will be ignored. – Andreas Jun 15 '16 at 22:19