0

I'm trying to run a command line in a mac terminal using java code (particularly a ProcessBuilder).

I'm coding in a Windows environment and then exporting it to a Mac into a Jar Executable File.

I managed to open the terminal using the code posted below, but once there, the commands are not running, the terminal keeps dead, waiting for a keyboard input.

I will show a simple example for what I want to do:

String[] arguments = new String[] {"/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal", "-c", "pwd"};
Process proc = new ProcessBuilder(arguments).start();

The code seems to run fine until it reaches the "-c" parameter, and then it does nothing else.

Does someone know how to solve this problem? Thanks!

EDIT: I've also tried without the "-c", but same result.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Possible duplicate of [Running a command in a new Mac OS X Terminal window](https://stackoverflow.com/questions/989349/running-a-command-in-a-new-mac-os-x-terminal-window) – that other guy Apr 19 '19 at 20:43
  • The reason why this doesn't work from Java is that it doesn't work from anywhere. You should first find a command that *can* run a program in a new terminal, and only then try to invoke that from Java. – that other guy Apr 19 '19 at 20:44

1 Answers1

0

Try this:

 try {
  Runtime.getRuntime().exec("your command");
 } catch (IOException e) {
    e.printStackTrace();
 }
  • Ok, tried like this Runtime.getRuntime().exec("/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal -c pwd"), and also without the "-c" parameter. Same result, the bash seems not to be getting the command after it opens. – Julian Buranits Apr 11 '19 at 14:29