Java can';运行外部命令时,无法获取完整错误
我正在尝试用java编译文件 以下是一个有错误的“.ts”文件:Java can';运行外部命令时,无法获取完整错误,java,process,cmd,Java,Process,Cmd,我正在尝试用java编译文件 以下是一个有错误的“.ts”文件: alert("hello, typescript"); errrrrrrrrrrrrrrrrrrrrrrrrrror 在windows shell(cmd)中编译时: 它将报告错误并显示以下消息: E:/WORKSPACE/test/typescripts/hello.ts(2,0): The name 'errrrrrrrrrrrrrrrrrrrrrrrrror' does not exist in the current
alert("hello, typescript");
errrrrrrrrrrrrrrrrrrrrrrrrrror
在windows shell(cmd)中编译时:
它将报告错误并显示以下消息:
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安装类型脚本您尝试过使用原始流程/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);
}
}
我只是花了几个小时研究同样的问题
最后,我通过在命令行中添加
“2>errorfile.txt”
解决了这个问题。这会将stderr
重定向到一个文件,然后我读取并打印该文件 试试apache,我想这会对你有所帮助。恐怕这是typescript的编译器tsc
的问题。由于我尝试了Java以外的一些工具(grunt.js),它们也只能得到第一行错误消息。我只是确保MS提供的tsc.exe
没有这样的问题,我在这个问题中运行的是从npm安装的tsc.cmd
,也看到这个问题:如果有帮助,这里有一个从java执行typescript编译的项目:+1我真的很想知道,为什么没有使用BufferedReader的解决方案当我尝试此解决方案时,它引入了另一个我无法处理的错误:您需要用CMD编码替换“UTF-8”。参见示例:
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);
}
}