2

I'm writing a Typescript programmatically in a Java based app. This produces a script.ts, which in normal Node js environment (whith Typescript installed) compiled and runs by:

tsc script.ts

My question is now: I can preinstall node js en Typescript on same server as the Java app, How can I then run from Java this script (mimic tsc script.ts)? And read the console log of it?

EDIT 1: I am trying to run the script.ts like following but encounter an error

p = Runtime.getRuntime().exec("tsc C:\\Users\\SK\\Documents\\Men\\HAI-main\\Executed\\script.ts");

Error:

Caused by: java.io.IOException: Cannot run program "tsc": CreateProcess error=2, The system cannot find the file specified
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
        at java.lang.Runtime.exec(Runtime.java:620)
3
  • 4
    silly question, but why use typescript in a programmatically generated script? if there is no developer, is there a need for type-safety? Commented Apr 11, 2019 at 19:01
  • because the vendor has a typescrip sdk, i have to, in order to speed up things Commented Apr 11, 2019 at 19:15
  • and you want to run the typescript from java why? Commented Apr 11, 2019 at 21:34

1 Answer 1

1

In order to compile the TS on the same server as your Java app, you just need to make sure Node, NPM, and Typescript are all installed. You will also need to make sure tsc is in your PATH or use the full path to the tsc executable in the code below.

Then you can just spawn a tsc process. Something like this:

Runtime.getRuntime().exec("tsc /path/to/script.ts");
Sign up to request clarification or add additional context in comments.

5 Comments

Okay and how can i get the console logging produced while running the script ?
The result of exec is a Process object. I'm guessing you can use its getOutputStream method and pipe the output stream to stdout or a file. See: docs.oracle.com/javase/7/docs/api/java/lang/Process.html
I wanted to compile tsc with p = Runtime.getRuntime().exec("tsc C:\\Users\\SK\\Documents\\Men\\HAI-main\\Executed\\script.ts"); but getting this error; any idea why? everything is installed in that folder node, npm, typescript... Caused by: java.io.IOException: Cannot run program "tsc": CreateProcess error=2, The system cannot find the file specified at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048) at java.lang.Runtime.exec(Runtime.java:620) I got this error now, I have installed everything in the folder
Sounds like tsc isn't in your path. Try passing the absolute path to exec. Something like this: exec("/path/to/tsc /path/to/script.ts")
I did, now i got this error: %1 is not a valid Win32 application1 seen the tsc is not an exe file....i found old topics which are saying that it has 2 executables not sure if that is still actual link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.