简体   繁体   English

AppleScript 命令在终端中有效,但在 Java 中无效

[英]AppleScript Command Works in Terminal but not in Java

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;我的主要问题是如何使下面提到的命令在 java 中工作,该命令的主要问题是它不执行我命令它执行的脚本; can someone help me make this apple script work in java?有人可以帮我让这个苹果脚本在 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).在终端中执行,但是每当我尝试使用 Runtime.getRuntime().exec() 命令在 java 中运行它时,它根本不会执行(它没有向我显示一个新的终端 window 代码正在执行,但在终端中工作)。 The whole point of this command is to show a new terminal with the output of the main.cpp file.该命令的全部意义在于显示一个带有 main.cpp 文件的 output 的新终端。 This is my java code:这是我的 java 代码:

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.为什么会这样,我该如何解决这个问题,以便我的脚本也可以在 javafx 中运行。 The point of this command is to show a new terminal window and then run a script.该命令的重点是显示一个新的终端 window 然后运行一个脚本。

I thought that I am running multiple commands correctly as listed in my comm variable and I am executing the command with runtime.我认为我正在正确运行我的comm变量中列出的多个命令,并且我正在使用运行时执行命令。

Thank you in advance!先感谢您!

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. Runtime.getRuntime().exec()不是在 Java 中运行和管理外部进程的最佳方式,最好使用java.lang.ProcessBuilder代替。

So your code can be expressed in java.lang.ProcessBuilder like this.所以你的代码可以像这样用java.lang.ProcessBuilder表示。

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
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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