1

My main question is how can I make the command mentioned below work in java, The main problem in this command is it does not do script that I commanded it to execute; can someone help me make this apple script work in java?

I do not understand but for some reason the command:

osascript -e 'tell application "Terminal" to do script "cd /Users/benjaminsloutsky/eclipse-workspace/stack/Stack/StackProjects/Brother/build && cmake .. && cmake --build . && ./cmake-good"'

executes in the terminal, but whenever I try to run it in java using Runtime.getRuntime().exec() command it does not execute at all (it does not show me a new terminal window with code being execucted, but works in terminal). The whole point of this command is to show a new terminal with the output of the main.cpp file. This is my java code:

final String innerCommand = 
     "cd /Users/benjaminsloutsky/eclipse-workspace/stack/Stack/StackProjects/Brother/build && cmake .. && cmake --build . && ./cmake-good";
String[] comm = new String[] {
     "/bin/bash", "-c", 
     "osascript -e 'tell application \"Terminal\" to do script \"" +
     innerCommand + "\"'"
};
Runtime.getRuntime().exec(comm);

it does not run. Why is that and how could I fix this issue so my script could also run in javafx. The point of this command is to show a new terminal window and then run a script.

I thought that I am running multiple commands correctly as listed in my comm variable and I am executing the command with runtime.

Thank you in advance!

15
  • 2
    Change "\"'" to "\"" in your Java code. The ' should not be there. Commented Apr 25, 2021 at 4:36
  • 1
    Ah. OK, I see now. But I had to fix your overly long Java lines to make it clear what was going on. Commented Apr 25, 2021 at 4:46
  • 1
    Is the terminal window being opened behind the current window; see apple.stackexchange.com/questions/205143 Commented Apr 25, 2021 at 4:55
  • 1
    Try getting it to just open a terminal and echo something. Commented Apr 25, 2021 at 5:46
  • 1
    You figured wrong. Reposting a question is a major no-no. Reposting is interpreted by people here as "attention seeking behavior". You are liable to find that the repost heavily downvoted and then dup-closed. The (only) legitimate way to draw attention to a question on StackOverflow is to post a Bonus. Commented Apr 25, 2021 at 10:21

1 Answer 1

2
+50

Runtime.getRuntime().exec() is not the best way to run and manage the external processes in Java, its better to use the java.lang.ProcessBuilder instead.

So your code can be expressed in java.lang.ProcessBuilder like this.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class CommandExecutor {

    public static void main(String[] args) {
        CommandExecutor executor = new CommandExecutor();
        executor.executeInTerminal("cd /Users/benjaminsloutsky/eclipse-workspace/stack/Stack/StackProjects/Brother/build && cmake .. && cmake --build . && ./cmake-good");
    }

    public void executeInTerminal(String command){
        List<String> cms = new ArrayList<>();
        cms.add("/bin/sh");
        cms.add("-c");
        cms.add("osascript -e 'tell application \"Terminal\" to do script \"" + command + "\"'");

        ProcessBuilder processBuilder = new ProcessBuilder();
        processBuilder.command(cms);
        try {

            Process process = processBuilder.start();

            StringBuilder output = new StringBuilder();

            BufferedReader reader = new BufferedReader(
                new InputStreamReader(process.getInputStream()));

             String line;
             while ((line = reader.readLine()) != null) {
                output.append(line + "\n");
            }

            boolean success = process.waitFor(1, TimeUnit.MINUTES);
            // check the status
        } catch (IOException | InterruptedException e) {
            // do some logging
        }
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

How come this works perfectly in java but not in javafx?
@BenjaminSloutsky there should not be any difference, do you have a code snippet which shows how are you executing it ?
@BenjaminSloutsky are you running in main thread ?
That can be a problem, try with Platform.runLater
That can be a problem with your IO, try processBuilder.redirectOutput(ProcessBuilder.Redirect.PIPE);processBuilder.redirectErrorStream(true); or processBuilder.inheritIO().start() otherwise share a code which does not work.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.