gpt4 book ai didi

运行外部命令时Java无法获取完整错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:30:38 25 4
gpt4 key购买 nike

我正在尝试编译 typescript Java 中的文件。

这是一个有错误的“.ts”文件:

alert("hello, typescript");
errrrrrrrrrrrrrrrrrrrrrrrrrror

当我在 windows shell(cmd) 中编译时:

tsc hello.ts

它会报告错误信息:

E:/WORKSPACE/test/typescripts/hello.ts(2,0): The name 'errrrrrrrrrrrrrrrrrrrrrrrrror' 
does not exist in the current scope

但是当我用java做的时候:

String cmd = "cmd /C tsc hello.ts";
Process p = Runtime.getRuntime().exec(cmd);
String out = IOUtils.toString(p.getInputStream());
String error = IOUtils.toString(p.getErrorStream());
System.out.println("### out: " + out);
System.out.println("### err: " + error);

它打印:

### out:
### err: E:/WORKSPACE/test/typescripts/hello.ts(2,0):

您可以看到未捕获详细错误。我的代码哪里有问题?


更新

我只是确定MS提供的tsc.exe没有这个问题,而我在这个问题中运行的是从npm安装的tsc.cmd npm 安装 typescript

最佳答案

您是否尝试过使用原始 Process/ProcessBuilder 组合?

ProcessBuilder pb = new ProcessBuilder("cmd /C tsc hello.ts");

//merge error output with the standard output
pb.redirectErrorStream(true);

Process p = pb.start();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream(), Charset.forName("UTF-8")))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}

关于运行外部命令时Java无法获取完整错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14534511/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com