0

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.

Community
  • 1
  • 1
  • you want a java class to start a terminal and then start a new JVM with another class? Why? Perhaps a case of the XY problem? http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem – muttonUp Sep 05 '16 at 16:31

1 Answers1

1

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});
    }
}
Community
  • 1
  • 1
Guenther
  • 2,035
  • 2
  • 15
  • 20
  • I tryed doing this: `code` Runtime.getRuntime().exec(new String[]{"/usr/bin/xterm","java -Dfile.encoding=UTF8 -jar \"" + filename + "\""} `code` But nothing happened. If i run using terminal, it works, but if I double click the jar, it does nothing. – Guilherme Ruiz Sep 05 '16 at 19:17
  • Thank you for your answer. I was able to make it work for linux and tried following your instructions for mac, but there are two problems: I need to provide arguments for java to run my program, not only run java from terminal and if I use ` Runtime.getRuntime().exec("/usr/bin/open -a Terminal /usr/bin/java");` it only runs java without any argument and exit the terminal. I tried `Runtime.getRuntime().exec("/usr/bin/open -a Terminal /usr/bin/java -jar " + filename ); but it does nothing. – Guilherme Ruiz Sep 06 '16 at 17:18
  • I'm doing this: `if(nameOs.startsWith("Mac")){ Runtime.getRuntime().exec(new String[]{"/usr/bin/open", "-a", "Terminal", "/usr/bin/java", "-jar", filename }); }` Where `filename = Main.class.getProtectionDomain().getCodeSource().getLocation().toString().substring(6); String nameOs=System.getProperty("os.name");` But it still doesn't work. – Guilherme Ruiz Sep 07 '16 at 16:00
  • Can you describe in detail, what happens? Does it not run at all, does it give an error message? Did you try to put the command "java -jar jarfile.jar" in a file and start that file? Does passing an argument to java work, i.e. did you try "/usr/bin/open", "-a", "Terminal", "/usr/bin/java", "-v" to print the jvm version? Is the jar-file at the correct location? Does the filename to java include special characters or spaces? Please provide as much information as possible on what you are doing. Otherwise it's difficult to help. – Guenther Sep 08 '16 at 05:16
  • It doesn't run at all. if I use "/usr/bin/open", "-a", "Terminal", "/usr/bin/java" it runs as if I had called java in terminal and exited, but if I use any argument it doesn't even open the terminal. The jar file is in the correct location and using "java -jar jarfile.jar" in a executable file works, but it doesn't help because the path to my file is to be defined within my program as I explained in the comment before. the filename does not have special characters. It works fine in linux and windows. Apparently the problem is mac syntax as the issue beggins when I try passing arguments. – Guilherme Ruiz Sep 08 '16 at 15:49
  • It seems that MacOS Terminal cannot start commands and pass arguments to the started commands. You need to use the workaround described in my answer. Create a temporary script file from Java which executes java with the desired command line arguments and then start terminal to execute it. – Guenther Sep 09 '16 at 09:42
  • I see... But how do I create a temporary script file from java? – Guilherme Ruiz Sep 09 '16 at 17:10
  • @GuilhermeRuiz See Edit 4 in my answer above – Guenther Sep 09 '16 at 18:38
  • Thank you very much! The script solution worked perfectly. – Guilherme Ruiz Sep 09 '16 at 22:51