Arwa
Arwa

Reputation: 587

Calling Java program from Ruby

I want to call Java program from my Ruby script. I am trying to use JRuby, I installed it and I am trying to see how it works.

I started by doing the following simple Java class:

package test;

public class Test {

public static void main(String[] args) {
 say();
}

public static void say(){
  System.out.println("oh hi!");  
}
}

And the following ruby program,

#! /usr/bin/env ruby

require 'java'
require '/Users/arwa/NetBeansProjects/test/dist/test.jar' 

class Main
 def run
  sayObj = Java::test::test.new
  sayObj.say()
 end
end

app = Main.new
app.run

In terminal, when I type

jruby test_again.rb

I get nothing! I don't know what is the problem.

Upvotes: 4

Views: 6083

Answers (1)

Christian MICHON
Christian MICHON

Reputation: 2180

You have issues in both java and ruby.

java code: main and 'static' removed

package test;

public class Test {

  public void say() {
    System.out.println("oh hi!");
  }

}

ruby code: you did not respect jruby uppercase when calling java libraries

#! /usr/bin/env ruby

require 'java'
require '/Users/arwa/NetBeansProjects/test/dist/test.jar' 

class Main
 def run
  sayObj = Java::Test::Test.new # NOTE the uppercase
  sayObj.say()
 end
end

app = Main.new
app.run

Upvotes: 3

Related Questions