简体   繁体   中英

Opening a jar file in a new terminal from existing java gui program

I have a Java GUI program, I want to run another jar file in a new terminal (which does not have a GUI) from this program whenever the user clicks a specified button. Can someone suggest how can I do this?

Starting new terminal requires spawn new process using ProcessBuilder or System.exec. And this depends on operating system and its configuration. In linux xterm running standalone jar file is started like this:

 File dir = new File("/directory/to/the/jar/file");
    String jarName ="standalone.jar";
    ProcessBuilder processBuilder = new ProcessBuilder("/usr/bin/xterm","-e","java -jar "+jarName);
    processBuilder.directory(dir);
    processBuilder.start();

If there is no standalone jar, then class path has to be spesified using -cp -parameter to the java excutable...

Alternative might be just run the given jar using separated ClassLoader and redirecting System.out and System.in. This requires writing some kind of GUI for the running jar. If only the output of the process is required and no terminal emulation is required this is not hard. Just redirecting System.out to some textarea. If there are more requirements for the input / output for the running then it might be possible to use terminal emulator written in java to do these things for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM