-2

Is it possible to compile & run scala code dynamically within Java code.

It is possible to achieve a similar result with JS using mozilla rhino. But, I wonder if it is possible with scala?

aunlu
  • 1
  • 2

2 Answers2

1

Theoretically, yes. But you will need to do a lot of things:

  • ensure the user entered valid scala code
  • transfer that source code to the server
  • compile the scala code
  • run it from within your server (catch errors, deal with resource leaks, ...)

So, possible: yes. Reasonable: not so much.

  • Obviously: a lot of work
  • getting to a decent user experience: even more work (like: telling the user exactly where in his source code input your compilation step found a bug ... hard)

And of course: opens your system for a ton of attack vectors.

If you want your users to be able to run code on the backend server, one wonders: why don't they have admin access to that machine already, and are able to deploy there code right there on the server themselves?!

Sure, Scala has its REPL, and as that one comment pointing to an existing answer implies: it is definitely possible to do that.

But as said: we don't do things because we can, but because it makes sense doing it!

GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

twitter util-eval library seems provide what I need, but it is discontinued.

Here is an old fork: https://github.com/m3dev/twitter-util-eval

aunlu
  • 1
  • 2