-2

请帮助我找出哪个函数用于在目标 c 中运行终端命令,就像我在以下代码中的 java 中使用的那样。我只想知道 obj c 或 cocoa 中使用哪个函数来执行终端进程。

Runtime rt = Runtime.getRuntime();
        Process prcComile = rt.exec("javac -d F:/ F://" + fname.getText()+ ".java");
        InputStream iscmp = prcComile.getErrorStream();
        int cerrInt = iscmp.read();
        if (cerrInt == -1) {
            Process prc = rt.exec("java -cp  F:/ " +fname.getText());
            InputStream iserr = prc.getErrorStream();
            int errInt = iserr.read();
            if (errInt == -1) {
                InputStream is = prc.getInputStream();
                int readInt = is.read();

                String allOutput = "";
                while (readInt != -1) {
                    char ch = (char) readInt;
                    allOutput = allOutput + ch;
                    readInt = is.read();
                }

                txtOutput.setText(allOutput);
            } else {
                String errorString = "";
                while (errInt != -1) {
                    char ch = (char) errInt;
                    errorString += ch;
                    errInt = iserr.read();
                }
                txtOutput.setText(errorString);
            }
        } else {
            String errorString = "";
            while (cerrInt != -1) {
                char ch = (char) cerrInt;
                errorString += ch;
                cerrInt = iscmp.read();
            }
            txtOutput.setText(errorString);
        }
4

1 回答 1

0

您可以使用 NSTask。应将 java 版本打印到标准输出(未读)的示例(未经测试):

- (IBAction)speak:(id)sender
{
    NSTask *task = [[NSTask alloc] init];
    task.launchPath = @"java";
    task.arguments  = @[@"-v"];
    [task launch];
    [task waitUntilExit];
    //use task.standardOutput to read
}
于 2013-10-29T18:27:46.123 回答