4

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.

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.