6

I know that I can use Lua Script files to manipulate Java Objects by using libraries like LuaJava. I had this idea of using C# scripts instead~

Is it possible to run C# scripts inside Java?

7
  • C# isn't really a "scripting" language. Commented Jul 1, 2011 at 14:12
  • For that matter, strictly speaking neither is Java. Javascript is, Java is not, and there is no "C#script" counterpart. Commented Jul 1, 2011 at 14:13
  • 2
    @StriplingWarrior Of course it is! Commented Jul 1, 2011 at 14:14
  • @StriplingWarrior - Doesn't really matter. I've read of cases when studying game design of engines injecting the JVM and using Java as a scripting language (note that I said Java, not Javascript!) Commented Jul 1, 2011 at 14:19
  • @TEG, try telling those crazy folks at BeanShell that Java isnt a scripting language ;). +1 to OP, great question and if you find something please let us know. Imagine LINQ yumminess in the JVM. Gimme! Commented Jul 1, 2011 at 14:22

3 Answers 3

6

In theory, yes - you can certainly do this in .Net applications and there are Java / .Net interops.

Typically however Java / C# interops are performed through either P/Invoke or COM - both are pretty cumbersome for this sort of thing and so in reality this probably won't work as neatly as you might have imagined.

All the same if you did want to do this I'd probably recommend that you write the "scripting engine" (i.e. wrapper around the C# compiler) in C#, and then have that expose it to Java land via interops, for example:

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

You then need to think carefully about how your C# scripts are going to be able to access any Java-land functionality, again I imagine the best way would be to implement a .Net wrapper class that calls Java objects through interops.

Using C# as a scripting language from within a .Net application is surprisingly straightforward - for information see:

Sign up to request clarification or add additional context in comments.

Comments

2

Are C# programs "scripts"? Regardless, you could run most all outside programs from via Runtime.exec(...), but be sure to watch for traps: When Runtime.exec() won't.

Things get a bit more tricky if you wish to have two-way communication between C# and Java, which can be done via simple sockets/streams or all the way up to COM interfaces.

Comments

2

You can do it the other way around. Have a look at http://www.ikvm.net/ - it allows object/library reuse from one language in the other.

Comments

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.