Java cannot get full error when running external command
I am trying to compile typescript files to java.
Here is the ".ts" file with errors:
alert("hello, typescript");
errrrrrrrrrrrrrrrrrrrrrrrrrror
When I compile in windows shell (cmd):
tsc hello.ts
It will report an error with the message:
E:/WORKSPACE/test/typescripts/hello.ts(2,0): The name 'errrrrrrrrrrrrrrrrrrrrrrrrror'
does not exist in the current scope
But when I do this in 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);
It prints:
### out:
### err: E:/WORKSPACE/test/typescripts/hello.ts(2,0):
You can see that part errors are not captured. Where is wrong with my code?
Update
I just made sure that the tsc.exe
one provided by MS does not have such an issue and the one I run in this question is the tsc.cmd
one installed from npmnpm install typescript
+3
source to share
2 answers
Have you tried using the raw Process / ProcessBuilder combination?
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
source to share