0

I saw Calling a groovy script from a java function using javax.script I guess with the runWithGroovyClassLoader can get a java class, java object and call its method with arguments. Though in the example its a no args example.

I a solution to be able to call type script. So my users enter type script in the front end. In the back end we call the script from within a Java function, passing it some state (arguments) of current txn from Java and then the type script returns a map / object back to calling java function

lepe
  • 24,677
  • 9
  • 99
  • 108
tgkprog
  • 4,493
  • 4
  • 41
  • 70
  • 1
    How come negative votes and close votes without a comment explaining why? – tgkprog Feb 04 '18 at 19:02
  • I just checked, and FWIW the close vote says this is asking for a lib recommendation. Which I can kind of see, the lib being something that implements a TypeScript engine for the Java Scripting API... – T.J. Crowder Feb 04 '18 at 19:04
  • @t-j-crowder but your answer is anything but that. Cant there be other solutions? Is this a bad question? – tgkprog Feb 04 '18 at 19:06
  • 1
    I wouldn't worry about it. There are **lots** worse ones. – T.J. Crowder Feb 04 '18 at 19:13
  • Well negative votes mean less people see it so less chance of getting any answers. its disheartening @t-j-crowder – tgkprog Feb 04 '18 at 19:38

1 Answers1

4

You'd be looking for a TypeScript engine that was compatible with the Java Scripting API. You probably won't find one, but you might.

However: The JDK ships with a JavaScript engine (Nashorn). If you transpile your TypeScript to JavaScript (via the TypeScript compiler, tsc), you can then run the resulting JavaScript via javax.script. You'll need to ensure that tsc is targeting "ES5", I don't think Nashorn supports ES2015+ yet.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Yes I know. I wanted to avoid this extra trip to node js to do this. Was hopingh to keep things between user, browser and my jvm. Your solution will work, just one more hop and one more server to maintain. – tgkprog Feb 04 '18 at 19:04
  • @tgkprog: I don't see why an extra hop or server is required. If you need to receive the code as TypeScript, you can easily have your Java code spawn `tsc` and run the result. – T.J. Crowder Feb 04 '18 at 19:06
  • Right I did not think of that. Great let me try that – tgkprog Feb 04 '18 at 19:07