Running a Gosu program from within Java

608 views
Skip to first unread message

Jeff Klein

unread,
Feb 15, 2012, 3:13:03 PM2/15/12
to gosu...@googlegroups.com

I am dabbling with gw.lang.reflect.ReflectUtil from open source Gosu version 0.9-3 in an attempt to reflectively instantiate a Gosu class, and invoke a method on it. However, I’m getting the following error when I try to run it.

 

Exception in thread "main" java.lang.RuntimeException: Current module must not be null.

       at gw.internal.gosu.parser.TypeLoaderAccess.returnFirstNonNullByName(TypeLoaderAccess.java:1152)

       at gw.internal.gosu.parser.TypeLoaderAccess.getTypeByFullNameIfValid(TypeLoaderAccess.java:572)

       at gw.internal.gosu.parser.TypeLoaderAccess.getTypeByFullNameIfValid(TypeLoaderAccess.java:539)

       at gw.internal.gosu.parser.TypeLoaderAccess.getIntrinsicTypeByFullName(TypeLoaderAccess.java:519)

       at gw.internal.gosu.parser.TypeSystemImpl.getByFullName(TypeSystemImpl.java:224)

       at gw.lang.reflect.TypeSystem.getByFullName(TypeSystem.java:145)

       at gw.lang.reflect.ReflectUtil.construct(ReflectUtil.java:19)

       at gw.lang.reflect.ReflectUtil.constructGosuClassInstance(ReflectUtil.java:39)

       at InvokeGosuHello.main(InvokeGosuHello.java:14)

 

Here’s a very simple test case:

 

Gosu side: (GosuHello.gs)

 

class GosuHello {

    function helloWorld() {

        print("Hello from Gosu!")

    }

}

 

Java side: (InvokeGosuHello.java)

 

import gw.lang.reflect.ReflectUtil;

import gw.lang.reflect.gs.IGosuObject;

 

public class InvokeGosuHello {

  public static void main(String[] args) {

    IGosuObject gosuObj = ReflectUtil.constructGosuClassInstance("GosuHello");

    ReflectUtil.invokeMethod(gosuObj, "helloWorld", null);

  }

}

 

Any thoughts would be appreciated.

 

Thanks,

Jeff Klein

Vyktur with a Y

unread,
Feb 15, 2012, 3:31:25 PM2/15/12
to gosu...@googlegroups.com
Also, if the driver is Java, you will probably have to init Gosu manually.

On Wed, Feb 15, 2012 at 12:30 PM, Vyktur with a Y <roxchk...@gmail.com> wrote:
I think you're going to need to put that .gs file in a package and reference it with the whole path.

--
You received this message because you are subscribed to the Google Groups "gosu-lang" group.
To post to this group, send email to gosu...@googlegroups.com.
To unsubscribe from this group, send email to gosu-lang+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/gosu-lang?hl=en.


Vyktur with a Y

unread,
Feb 15, 2012, 3:30:46 PM2/15/12
to gosu...@googlegroups.com
I think you're going to need to put that .gs file in a package and reference it with the whole path.
On Wed, Feb 15, 2012 at 12:13 PM, Jeff Klein <je...@jeffklein.org> wrote:

--

Alan Keefer

unread,
Feb 15, 2012, 4:12:00 PM2/15/12
to gosu...@googlegroups.com
Yeah, the easiest way to do things is to use a .gsp file as the driver
and then execute the .gsp file using Gosu. If you want to use Java as
the driver, you have to call Gosu.init() before referencing any Gosu
types or executing any code.

-Alan

On Wed, Feb 15, 2012 at 12:31 PM, Vyktur with a Y

Brian Chang

unread,
Feb 15, 2012, 4:12:55 PM2/15/12
to gosu...@googlegroups.com
Call Gosu.init() in your main() method.

Brian Chang

unread,
Feb 15, 2012, 4:16:21 PM2/15/12
to gosu...@googlegroups.com
BTW, Jeff, since you're a Maven guy, I think you'll be delighted to learn that it's very convenient to do your stuff in a Maven-structured project and pull the Gosu dependencies.  See https://github.com/gosu-lang/Goson for a barebones example.

Jeff Klein

unread,
Feb 15, 2012, 5:25:19 PM2/15/12
to gosu...@googlegroups.com

Thanks to everyone for your replies.  I scoped my .gs file to a package (org.jeffklein.gosu), and added Gosu.init(). I am now able to instantiate the Gosu class, but I’m getting an NPE when it tries to invoke the method. Seems like I’m getting close!

 

Exception in thread "main" java.lang.NullPointerException

org.jeffklein.gosu.GosuHello@136a1a1

                at gw.lang.reflect.ReflectUtil.extractRuntimeTypes(ReflectUtil.java:222)

                at gw.lang.reflect.ReflectUtil.invokeMethod(ReflectUtil.java:107)

                at org.jeffklein.gosu.InvokeGosuHello.main(InvokeGosuHello.java:13)

 

The java source now looks like:

 

package org.jeffklein.gosu;

import gw.lang.Gosu;

import gw.lang.reflect.ReflectUtil;

import gw.lang.reflect.gs.IGosuObject;

 

public class InvokeGosuHello {

  public static void main(String[] args) {

    Gosu.init();

    IGosuObject gosuObj = ReflectUtil.constructGosuClassInstance("org.jeffklein.gosu.GosuHello");

    System.out.println(gosuObj);

    ReflectUtil.invokeMethod(gosuObj, "helloWorld", null);

Jeff Klein

unread,
Feb 15, 2012, 5:32:18 PM2/15/12
to gosu...@googlegroups.com

Thank you for the pointer to the Goson project Brian. My project is indeed a maven project, and I am pulling in my Gosu dependencies from an internal repo (only because I didn’t know about the public repo @gosu-lang.org). I’ve changed my repository setting in my pom to match that in the Goson project pom:

 

  <repositories>

    <repository>

      <id>gosu-lang.org-releases</id>

      <name>Official Gosu website (releases)</name>

      <url>http://gosu-lang.org/repositories/m2/releases</url>

    </repository>

  </repositories>

 

There are much more current versions of my gosu dependencies on the public site. Thank you again for the tip!!

 

Jeff

 

 

From: gosu...@googlegroups.com [mailto:gosu...@googlegroups.com] On Behalf Of Brian Chang


Sent: Wednesday, February 15, 2012 1:16 PM
To: gosu...@googlegroups.com

Jeff Klein

unread,
Feb 15, 2012, 6:13:55 PM2/15/12
to gosu...@googlegroups.com

Thanks to everyone for your replies.  I scoped my .gs file to a package (org.jeffklein.gosu), and added Gosu.init(). I am now able to instantiate the Gosu class, but I’m getting an NPE when it tries to invoke the method.

 

Exception in thread "main" java.lang.NullPointerException

org.jeffklein.gosu.GosuHello@136a1a1

                at gw.lang.reflect.ReflectUtil.extractRuntimeTypes(ReflectUtil.java:222)

                at gw.lang.reflect.ReflectUtil.invokeMethod(ReflectUtil.java:107)

                at org.jeffklein.gosu.InvokeGosuHello.main(InvokeGosuHello.java:13)

 

The java source now looks like:

 

package org.jeffklein.gosu;

import gw.lang.Gosu;

import gw.lang.reflect.ReflectUtil;

import gw.lang.reflect.gs.IGosuObject;

 

public class InvokeGosuHello {

  public static void main(String[] args) {

    Gosu.init();

    IGosuObject gosuObj = ReflectUtil.constructGosuClassInstance("org.jeffklein.gosu.GosuHello");

    System.out.println(gosuObj);

    ReflectUtil.invokeMethod(gosuObj, "helloWorld", null);

  }

}

 

 

 

From: gosu...@googlegroups.com [mailto:gosu...@googlegroups.com] On Behalf Of Brian Chang


Sent: Wednesday, February 15, 2012 1:16 PM
To: gosu...@googlegroups.com
Subject: Re: [gosu-lang:718] Running a Gosu program from within Java

 

BTW, Jeff, since you're a Maven guy, I think you'll be delighted to learn that it's very convenient to do your stuff in a Maven-structured project and pull the Gosu dependencies.  See https://github.com/gosu-lang/Goson for a barebones example.

Carson Gross

unread,
Feb 15, 2012, 9:15:41 PM2/15/12
to gosu...@googlegroups.com
Heya Jeff,

Can I ask what you are using to develop Gosu?  If you are running the Intellij plugin, you should be able to run a program by simply right clicking and running it.  Scott also recently added a bundler tool that creates an executable jar, but it looks like Victor is having some issues with that.

If you are developing from the command line, I'd recommend just using the gosu program as you entry point.

I'll play around a bit with what you are trying to do tonight and see if I can figure out what's going wrong: the last iteration you have seems right to me.  It's not a launch model we've put a lot of time into, as you can probably tell.  :)

Thanks for looking at Gosu!

Cheers,
Carson

Jeff Klein

unread,
Feb 15, 2012, 11:12:59 PM2/15/12
to gosu...@googlegroups.com

Hi Carson,

 

I am developing in IntelliJ Ultimate. I haven’t installed the Gosu plugin yet for that reason. I have been meaning to install a community edition of IJ so that I can try it out, but alas I haven’t gotten around to it yet.

 

Just FYI, the NPE was my bad. I was using the 3-arg version of invokeMethod(), and passing a null list of method arguments. Changing to the 2-arg version took care of the problem, and I can invoke Gosu from Java- Very cool! Again, thanks to everyone for your replies.

 

Cheers,

Jeff

Brian Chang

unread,
Feb 15, 2012, 11:42:40 PM2/15/12
to gosu...@googlegroups.com
Just bear in mind that the recommended entry is via a .gsp file.  It's a testament to Gosu's flexibility that you can do this, but you're making things harder on yourself.

Carson Gross

unread,
Feb 15, 2012, 11:42:56 PM2/15/12
to gosu...@googlegroups.com
Ah, varargs insanity strikes again.  Glad to hear you got it going.

Cheers,
Carson
Reply all
Reply to author
Forward
0 new messages