C # Scripting Inside Java

I know that I can use Lua Script files to manage Java objects using libraries like LuaJava . I had an idea to use C # scripts instead of ~

Is it possible to run C # scripts inside Java?

+6
source share
3 answers

Theoretically, yes, you can, of course, do this in .Net applications, and there are Java / .Net transitions.

Typically, however, Java / C # hooks are done through P / Invoke or COM - both are quite cumbersome for this kind of thing, and therefore, in fact, this probably won't work as neatly as you might imagine.

Anyway, if you want it, I would recommend that you write a "script engine" (i.e. a wrapper around the C # compiler) in C #, and then pass this to the Java ground via interops, for example:

public ScriptResult(string Script) { // Implemented in .Net // Script is a string containing the C# code to execute } 

Then you need to think carefully about how your C # scripts will have access to any Java-land functionality, and I think the best way is to implement a .Net wrapper class that calls Java objects through interops.

Using C # as a scripting language from a .Net application is surprisingly simple - see for more information.

+6
source

Are C # programs "scripts"? Despite this, you can run most external programs using Runtime.exec (...), but be sure to keep an eye on the traps: When Runtime.exec () won "t .

Things get a little more complicated if you want to have two-way communication between C # and Java, which can be done using simple sockets / threads or down to COM interfaces.

+2
source

You can do it the other way around. Take a look at http://www.ikvm.net/ - it allows the reuse of objects / libraries from one language to another.

+2
source

Source: https://habr.com/ru/post/891823/


All Articles