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.

9 comments:

sakesun 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

Cliff 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

Scott 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);

aiyipianni said...

Aston Villa rode their luck at Hull City where an 88-minute own goal from Kamil Zayatte saw them leapfrog three points clear of Arsenal and into fourth place in the Premier League wow gold with a 1-0 win.

Villa had to survive Hull penalty wotlk gold appeals for a handball against Ashley Young in time added on, television replays showing that referee Steve Bennett wow gold correctly rejected the claims after consulting a linesman.

Bennett had been involved in controversy after just five minutes when American goalkeeper Brad Friedel looked to have handed Hull the initiative and threaten Villa's return to the Champions League qualifying wow gold zone.

Friedel spilled wow gold the ball under pressure from Nick Barmby and stand-in right-back Nigel Reo-Coker turned it into his own net as he attempted to wow gold clear.

But Bennett cut short wow gold celebrations at the KC Stadium -- and let Friedel off the hook -- when he ruled out the score for an wow wotlkapparent infringement by Barmby.

Zayatte's intervention from a Young cross bound for wow gold Gabriel Agbonlahor then saw Villa leapfrog Arsenal and draw level with Manchester United on 38 points -- seven adrift of leaders Liverpool and four wow gold behind Chelsea.

Stung by an on-pitch dressing down wow gold by manager Phil Brown at Manchester City last week, Hull showed five changes and a vastly improved performance.

Promoted Hull were looking for only their second win in 11 games while wow gold Villa arrived unbeaten in seven and it looked to be heading for a goalless draw when the home side suffered a cruel late blow.

aiyipianni said...

South Africa inflicted the world of warcraft gold first home series defeat on Australia in almost 16 wow powerleveling years as they wrapped up a nine-wicket win over the world's number one ranked world of warcraft gold Test nation in Melbourne on Tuesday.

Captain Graeme Smith wow power leveling hit a fluent 75 as his side successfully passed a world of warcraft gold modest victory target of 183 on the final day at the MCG to take an wow powerleveling unassailable 2-0 lead.

It was the South dofus kamas African's first-ever Test series triumph in Australia and dofus kamas victory in the third and final match in Sydney will see them leapfrog the home side at the top of the global Lord of the Rings Online Gold rankings.

Hashim Amla LOTRO Gold (30 not out) scored the winning fly for fun penya runs shortly after lunch as South Africa flyff penya became the first team to overcome Australia at home Final Fantasy XI gilsince the West Indies in 1992-93.

South Africa ffxi gil were never under any pressure in eq2 plat their run chase and did not lose eq2 gold a wicket until just before lunch when the inspirational Smith Lord of the Rings Online Gold was trapped leg before wicket by Nathan LOTRO Gold Hauritz.

Smith had flyff penya dominated a 121-run opening stand flyff money with Neil McKenzie, hitting ffxi gil 10 boundaries.

McKenzie struggled to buy ffxi gil a half century and survived strong eq2 plat lbw shouts from Brett Lee, eq2 gold who was bowling despite an injured foot that will Lord of the Rings Online gold keep him out of the Sydney Test.

South Africa's LOTRO gold victory was set up by a brilliant maiden Test century fly for fun penya from JP Duminy, who shared a stunning flyff penya 180-run ninth wicket partnership with pace bowler Dale Final Fantasy XI gil Steyn.

It gave the tourists ffxi gil a priceless 65-run lead on first innings before man of eq2 plat the match Steyn worked his magic with the ball as Australia were eq2 gold bowled out on the fourth day for 247 in their second innings.

The pugnacious Smith was virtually runescape money lost for words in his victory speech.

"It has been such a special moment runescape gold for all of us, it has been an incredible team effort," he said.

"I have been smiling non-stop wow po since we hit the winning runs.

"To be 2-0 up after this game was something wow or we only dreamt of."

South Africa won the first Test in Perth from an unlikely position, chasing 414 for victory for the loss of only four wickets.