Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Java program from Ruby

Tags:

java

ruby

jruby

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.

like image 922
Arwa Avatar asked Sep 01 '25 01:09

Arwa


1 Answers

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
like image 197
Christian MICHON Avatar answered Sep 02 '25 14:09

Christian MICHON