【问题标题】:csc -version is running in terminal but not from my java program MACcsc -version 正在终端中运行,但不是从我的 java 程序 MAC 中运行
【发布时间】:2019-03-23 04:35:29
【问题描述】:
package test_cmd_command;

import java.io.BufferedReader;
import java.io.IOException; 
import java.io.InputStreamReader;
import java.time.LocalDateTime;


public class CommandLine {

public static String executeCommand(String cliCommand) {
    String s = null;
    BufferedReader stdInput = null;
    BufferedReader stdError = null;
    String error = "";
    String output = "";
    try {
        ProcessBuilder pb1 = new ProcessBuilder(
                "bash",
                "-c",
                cliCommand);

        pb1.redirectErrorStream(true);
        Process p = pb1.start();

        stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
        stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

        while ((s = stdInput.readLine()) != null) {
            output += "\n" + s;
        }
        //System.out.println("Here is the standard error of the command (if any):\n");
        while ((s = stdError.readLine()) != null) {
            //System.out.println(">> "+s.toString());
            error += "\n" + s;
        }

    } catch (IOException e) {
        System.out.println("exception happened - here's what I know: \n" + e.getMessage());
    } finally {
        try {
            stdInput.close();
            stdError.close();
        } catch (IOException e) {

        }
    }
    String returnValue = null;
    if (output != null && error != null) {
        returnValue = output + "\n" + ":  " + error;
    } else if (output != null) {
        returnValue = output;
    }
    return returnValue;
}
}

"csc -version" 正在终端中运行,但不是从我在 MAC 上的 java 程序中运行。 它给出输出“找不到bash命令”。 有什么办法可以解决这个问题...... 该程序正确运行其他命令,如 javac -version 等。 我在 MAC 而非 Windows 上运行此程序。

【问题讨论】:

  • 我猜 MacOS 的 bash 上没有“cmd”?在 bash 中你运行“csc -version”,但在你的代码中你实际上在 bash “cmd /c csc -version”中运行......你试过 Runtime.getRuntime().exec("cliCommand); 吗?只是猜测,对 MacOS 不熟悉。
  • @AccessDenied 它对我不起作用..

标签: java linux macos javafx


【解决方案1】:

这对我有用

export PATH=/Library/Frameworks/Mono.framework/Versions/Versions/bin/:${PATH}

我运行这样的命令 "export PATH=/Library/Frameworks/Mono.framework/Versions/Versions/bin/:${PATH}; csc -version" 它可以工作并返回版本的 csc。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-02
    • 2015-05-25
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 2017-10-24
    • 2014-02-05
    • 2020-04-01
    相关资源
    最近更新 更多