Skip to end of metadata
Go to start of metadata

Overview

It is possible to run Groovy on .NET 2.0 using IKVM as a virtual machine. By using IKVM compiler, you can compile your Groovy jar to be a .NET assembly (a DLL file) with the following command:

However, some warning will appear, but you can ignore them. After compilation you should have groovy-all-1.5.0.dll in the current directory.

Using the Groovy .NET Assembly

With the compiled DLL, you can use it as a normal .NET assembly. The following example shows how to add it as a reference for building a simple application. You need the SharpDevelop IDE in this case.

  • Open SharpDevelop, create a new .NET console application.
  • Add references, your groovy DLL, and all IKVM DLLs.
  • Open your main class, type the following code (in C# for example)
  • Run the program, and you'll also have an executable file as well (in ./Debug directory).

the original post: http://chanwit.blogspot.com/2007/04/groovy-running-fine-on-net.html

Invoking .NET classes

We can call .NET classes directly from Groovy scripts. Note that IKVM has the cli prefix for all .NET namespaces. If you want to invoke Console.WriteLine('hello world'), you have to type:

The great thing here is that Groovy can pick a correct .NET method. You can also try:

in the interactive console (the binary in the attachment of this page).

Encoding Issues

On some machines, there will be a test encoding issue to stop Groovy compiling script files. This seems to be the use of different encoding names between Java and .NET. If you find this kind of error, try specifying "-c" when testing with groovy.ui.GroovyMain class.

Further development as a Groovy module?

Discuss here.

2 Comments

  1. Indeed, yes, that would be nice to have it somehow as a module!

  2. Converting Groovy 1.7.5 to a DLL worked as described here (used ikvm 0.44.0.5).
    Running your example program yielded the following exception, though.

    "Der Typeninitialisierer für \"org.codehaus.groovy.runtime.InvokerHelper\" hat eine Ausnahme verursacht."

    bei org.codehaus.groovy.runtime.InvokerHelper.getMetaClass(Class cls)
       bei groovy.lang.GroovyObjectSupport..ctor()
       bei org.codehaus.groovy.tools.shell.util.MessageSource..ctor(String[] names)
       bei org.codehaus.groovy.tools.shell.util.MessageSource..ctor(Class type)
       bei groovy.ui.InteractiveShell..cctor()

    bei groovy.ui.InteractiveShell.main(String[] args)
       bei GroovyTest.Program.Main(String[] args) in c:\users\meona\documents\visual studio 10\Projects\GroovyTest\GroovyTest\Program.cs:Zeile 11.
       bei System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       bei System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       bei Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       bei System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       bei System.Threading.ThreadHelper.ThreadStart()