简体   繁体   中英

How do I make my jar file open a terminal window to run other class?

I'm trying to make a Jar file which runs a main class that opens a new terminal window and run other class from inside that Jar.

I saw this discussion " How do I make my java application open a console/terminal window? " and Brandon Barajas' answer was exactly what I was looking for, but only for windows. I know how to make the system detection but I can't make the program do the same for mac and linux. Can somebody (maybe Brandon) help me?

EDIT:

I was able to make it work in linux using this command Runtime.getRuntime().exec(new String[]{"/usr/bin/xterm","-e","java -Dfile.encoding=UTF8 -jar \\"" + "/" + filename + "\\""+"; bash"}); to open xterm and issue the command, but I still don't know how to make it work for mac terminal.

EDIT 2:

Now I can open mac terminal using Runtime.getRuntime().exec("/usr/bin/open -a Terminal /usr/bin/java"); but it still won't accept arguments to run my program. It only opens terminal, run java and exit. If i try using Runtime.getRuntime().exec("/usr/bin/open -a Terminal /usr/bin/java -jar" + filename); it does nothing.

You simple need to change the command that you need to start the java executable on the various platforms. For Linux this has already been answered here How to open a command terminal in Linux? For mac it's going to be similar.

EDIT 1

The linked answer uses the following arguments

String[] cmdArray = {"xterm", "-e", myScript + " ; le_exec"};
r.exec(cmdArray).waitFor();

The -e argument is important. It tells xterm to execute another command. Please read the answer carefully and adjust for your needs, also regarding "; le_exec." If xterm doesn't open, then check if the path and the permissions are correct.

EDIT 2/3 For mac, the question has already been answered here Open a new prompt/terminal window from Java

Runtime.getRuntime().exec("/usr/bin/open", "-a", "Terminal", "/usr/bin/java", "-jar", filename);

where executable is java in your case. Open a new prompt/terminal window from Java

If your filename (including the path) contains spaces, you might be running into a different problem, which has been dealt with here Why does Runtime.exec(String) work for some but not all commands? . I edited the command line for mac to use separate strings for the arguments. Can you try to see if that's working?

EDIT 4:

Alternatively, you could write the command "/usr/bin/java - jar 'your.jar'" to a script file, make it executable and execute it.

import java.io.IOException;
import java.io.PrintWriter;

public class TestClass {
    public static void main(String[] args) throws IOException {
        String jarFileName = "someJar.jar";
        String scriptFileName = "/tmp/script.sh";

        PrintWriter writer = new PrintWriter(scriptFileName, "UTF-8");
        writer.println("#!/usr/bin/env bash");
        writer.println("/usr/bin/java - jar '" + jarFileName + "'");
        writer.close();
        Runtime.getRuntime().exec("chmod u+x " + scriptFileName);
        Runtime.getRuntime().exec(new String[] {"/usr/bin/open", "-a", "Terminal", scriptFileName});
    }
}

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