0 Replies - 835 Views - Last Post: 08 February 2014 - 11:46 AM Rate Topic: -----

#1 RevTorA  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 76
  • View blog
  • Posts: 251
  • Joined: 22-April 11

Java class from within JRuby

Posted 08 February 2014 - 11:46 AM

I couldn't tell which forum to post this in. Trying to figure out how to embed some scripts in Java so I can run them from within a game engine I've been workin' on. JRuby looks promising, but not sure I can do what I want to do. At the moment I'm trying to run a Java class method from within a ruby script. I didn't know what the method would be renamed to in ruby so I ran a "methods" call to find it. It's not showin' up:

import org.jruby.embed.*;

public class RubyTest1 {

	private RubyTest1() {
		ScriptingContainer container = new ScriptingContainer();
		container.put("JavaClass", new JavaClass());
		
		String script = "puts JavaClass.methods";
		container.runScriptlet(script);
	}
	
	public static void main(String[] args) {
		new RubyTest1();
	}
}

class JavaClass {
	public void javaHelloWorld() {
		System.out.println("Hello World!");
	}
}



Any way I can run javaHelloWorld from the script? Also please let me know which forum I should post this in.

EDIT: I actually got it to recognize the class itself from within ruby but still can't seem to see the javaHelloWorld method.

import org.jruby.embed.*;

public class RubyTest1 {

	private RubyTest1() {
		ScriptingContainer container = new ScriptingContainer();
		//container.put("JavaClass", new JavaClass());
		
		String script = "require 'java'\n"
				+ "java_import 'JavaClass'\n"
				+ "puts JavaClass.methods";
		container.runScriptlet(script);
	}
	
	public static void main(String[] args) {
		new RubyTest1();
	}
}

class JavaClass {
	public static void javaHelloWorld() {
		System.out.println("Hello World!");
	}
}



EDIT 2: Solved! JavaClass needs to be public, apparently JRuby is implementing Java's visibilities now.

This post has been edited by RevTorA: 08 February 2014 - 12:44 PM


Is This A Good Question/Topic? 3
  • +

Page 1 of 1