3

我正在尝试在 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):

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


更新

我只是确保tsc.exeMS提供的没有这样的问题,我在这个问题中运行的是tsc.cmd从npm安装的npm install typescript

4

2 回答 2

2

您是否尝试过使用原始 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);
    }
}
于 2013-01-26T07:33:28.070 回答
0

我只是花了几个小时来解决同样的问题。

最后,我通过添加"2> errorfile.txt"到我的命令行来解决它。这会将 重定向stderr到一个文件,然后我读取并打印该文件。

于 2013-06-05T02:29:48.903 回答