Search code examples
javaruby-on-railsjrubyjrubyonrails

RoutingError (cannot load Java class hello.MyFirstJavaProgram)


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!


Solution

  • 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.