Saturday, April 28, 2007

Groovy running fine on .NET

It's just a crazy idea to test Groovy on .NET, after I read this post on IKVM weblog that the author test IKVM using JRuby. If JRuby runs fine, probably Groovy does so.

Simple steps here,

1. Download .NET 2.0 Runtime.
2. Download SharpDevelop IDE 2.1, just in case that you're lazy enough to code.
3. Download IKVM for .NET 0.34 rc2 binary
4. compile your Groovy jar with the following command:

ikvmc -target:library groovy-all-1.1-SNAPSHOT.jar

Some errors will appear, just ignore them.
You'll get groovy-all-1.1-SNAPSHOT.dll.

5. Open SharpDevelop, create a new .NET console application.
6. Add references, all IKVM DLLs and your groovy DLL.
7. Open your main class, type the following code (in C# for example)
using System;
using System.Collections.Generic;

namespace GroovyDotNet {
class MainClass {
public static void Main(string[] args) {
groovy.ui.InteractiveShell.main(args);
}
}
}

8. Run the program, and you'll also have an executable file as well.

Alright, if you don't want to get your hand dirty, do step 1 and wait for the binary. I'll upload it soon.

Update:
Here's the screen shot.


and here's the binary.

7 comments:

Sake said...

Error with Thai language setting...

Unhandled Exception: System.TypeInitializationException: The type initializer for 'org.codehaus.groovy.runtime.InvokerHelper' threw an exception. ---> System.TypeInitializationException: The type initializer for 'groovy.lang.GroovySystem' threw an exception. ---> System.TypeInitializationException: The type initializer for 'org.codehaus.groovy.runtime.DefaultGroovyMethods' threw an exception. ---> System.TypeInitializationException: The type initializer for 'java.util.logging
.Logger' threw an exception. ---> java.lang.Error: java.io.UnsupportedEncodingException: Encoding: windows-874 not found.
--- End of inner exception stack trace ---
at java.util.logging.Logger.getLogger(String name)
at org.codehaus.groovy.runtime.DefaultGroovyMethods..cctor()
--- End of inner exception stack trace ---
at System.Runtime.CompilerServices.RuntimeHelpers._RunClassConstructor(IntPtr type)
at System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(RuntimeTypeHandle type)
at IKVM.Internal.TypeWrapper.RunClassInit()
at IKVM.NativeCode.java.lang.VMClass.forName0(String name, Boolean initialize, Object classLoader)
at java.lang.VMClass.forName0(String , Boolean , ClassLoader )
at java.lang.VMClass.forName(String , Boolean , ClassLoader )
at java.lang.Class.forName(String name)
at org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl.class$(String)
at org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl..ctor(Int32 loadDefault, Boolean useAccessible)
at org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl..ctor()
at groovy.lang.GroovySystem..cctor()
--- End of inner exception stack trace ---
at org.codehaus.groovy.runtime.Invoker..ctor()
at org.codehaus.groovy.runtime.InvokerHelper..cctor()
--- End of inner exception stack trace ---
at org.codehaus.groovy.runtime.InvokerHelper.getMetaClass(Object object)
at groovy.lang.GroovyObjectSupport..ctor()
at groovy.lang.Binding..ctor()
at groovy.ui.InteractiveShell..ctor(InputStream in, PrintStream out, PrintStream err)
at groovy.ui.InteractiveShell..ctor()
at groovy.ui.InteractiveShell.main(String[] args)
at GroovyDotNet.MainClass.Main(String[] args)

chanwit said...

Hi sakesun,

That's very interesting bug of GNU Classpath. We, as Thais, should push it to the Classpath development community. You can report the bug here: http://gcc.gnu.org/bugzilla/.

Cheers,

Chanwit

Anonymous said...

Quick question, why do you need a wrapper class to call the Groovy main?

chanwit said...

Hi Cliff,

It's just the way .NET's working. You can run an EXE, but not a DLL.
You need an EXE file to invoke your Main class, not like in Java world where you can 'java -jar yourjar.jar'.

However, if you dont want to create a wrapper, I think you can use ikvm to start the program.

Cheers,

Chanwit

Anonymous said...

Hi Chanwit,

Thanks for trying this!

In response to the question about the startup wrapper, you can also let ikvmc generate it for you:

ikvmc -out:groovy.exe -r:groovy-all-1.1-SNAPSHOT.dll -main:groovy.ui.InteractiveShell

Regards,
Jeroen (ikvm author)

chanwit said...

Hello Jereon,

Thanks for a nice tip :)

Your IKVM is always excellent! I've been using it since 0.8.

Cheers,

Chanwit

scottw512 said...

Thanks. This is great. The InteractiveShell works great. The console displays but doesn't really work right. The code looks like this...

string []args2 = new string[]{"--main", "groovy.ui.Console",
"--conf", @"C:\installs\groovy-1.0\conf\groovy-starter.conf",
"--classpath", @"C:\installs\groovy-1.0\lib\groovy-starter.jar"};
org.codehaus.groovy.tools.GroovyStarter.main(args2);