将Java转换为Obj C.

时间:2013-10-29 18:17:41

标签: objective-c macos

请帮助我找出在目标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);
        }

1 个答案:

答案 0 :(得分: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
}