0

I am using jruby for the first time and i am trying to load a jar file which later on i will try to send parameters in and run some stuff.

I used a tutorial to write sample hello world java program using the code below

package test_pack;

public class MyFirstJavaProgram {

    public static void main(String []args) {
       System.out.println("Hello World");
    }
}

i place the helloworld.java file inside a test_pack folder and i then packed it using a command to produce a .jar file.

i placed the .jar file into the lib directory of rails, and used the following code to call it

class WebhookController < ApplicationController

  require "java"
  require "hello.jar"
  java_import "hello.MyFirstJavaProgram"
  Java::test_pack::MyFirstJavaProgram.main()


  def check

  end


end

but it gives me the error

cannot load Java class hello.MyFirstJavaProgram

obviously my path is wrong but i do not know how to fix it, any clues welcomed!

1 Answer 1

1
java_import "test_pack.MyFirstJavaProgram"

should do the trick.

PS. Also, in java world, you should name your file the same as your class name. In your case your filename should be MyFirstJavaProgram.java, not helloworld.java.

Sign up to request clarification or add additional context in comments.

5 Comments

Hello there, thanks for the notes but unfortunately i still get the same error. I also went and renamed the java file. Is there something else i can try??
Did you recompiled and repackaged your jar file and restarted the rails server?
by recompiled what do you mean? - repackaged the jar file and restarted the server yes . PS i am super new to java as well
EDIT: i took the compile thing into account, and it seems i had to also use the javac command repack with just the .class file and then replace it in rails,restart server and now the class is found but i get this error wrong number of arguments (0 for 1) when calling main() any clue on that?
As you can see, the main(String []args) method is expecting an array of String arguments. So, you have to pass an empty array to it.

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.