Using Custom Java Class file in Jruby - jruby

I am trying to execute some custom Java code through the latest version of Jruby (1.5.1), Ruby 1.8.7, with Java 1.6.0_06. I have tried both the class file and putting it in a jar method. When I try
require 'java'
require 'path_to_class/myClass
or
require 'java'
require 'path_to_jar/a_jar.jar
Trying both methods, I cannot access the myClass nor any other files in the jar file. Other variations on the net for importing java classes lead to the following error:
`NameError: cannot load Java class com.package.myClass from C:/jruby-1.5.1/lib/ruby/site_ruby/shared/builtin/javasupport/java.rb:51:in method_missing`
I have also checked the solutions on StackOverFlow and I still get the same outcome. I am wondering if this might be a problem at a deeper level.

Instead of 'require', you want 'java_import'.
require 'java'
java_import com.package.MyClass
See JRuby: import vs include vs java_import vs include_class for some more discussion e.g. why you should use 'java_import' instead of just 'import'

If you have a Java class com.mypackage.MyClass in the same folder, or in a folder present on the classpath, you can call it from your JRuby script like this:
require 'java'
import com.pack.MyClass
myClass = MyClass.new
If the class is in a jar, you have to require the jar:
require 'java'
require '/path/to/myjar.jar'
import com.pack.MyClass
myClass = MyClass.new
If myjar.jar is on the classpath, you can just use require 'myjar.jar'.

Did you try include Java?
See this for more details: http://blogs.oracle.com/coolstuff/entry/using_java_classes_in_jruby

So Here is what worked for me, I had all required stuff that people suggested but what I really needed was
$CLASSPATH << (Rails.root.to_s + "/path/to/dotClassFolder")
before the java_import statement
so in the file system, if your class was was in the folder
Rails.root/path/to/dotClassFolder/folder/anotherFolder/MyClass.class
Include $CLASSPATH << (Rails.root.to_s + "/path/to/dotClassFolder")
then java_import "folder.anotherFolder.MyClass"
See
From .class files section at https://github.com/jruby/jruby/wiki/CallingJavaFromJRuby

Related

jruby does not understand import statement

I'm converting a project written with JRuby 1.7 in mind to JRuby 9.4. One of the files starts like this:
if RUBY_PLATFORM == 'java'
require 'java'
import java.lang.management.ManagementFactory
....
end
The import does not work anymore. With JRuby 9.4, just compiling this file produces the error message NoMethodError: undefined method `import' for main:Object.
My understanding was that require 'java' would provide the import method, and at least it was working with JRuby 1.7.
In case it matters: We are using the imported class like this:
current_heap_in_bytes = ManagementFactory.getMemoryMXBean.getHeapMemoryUsage.used
On this page:
https://github.com/jruby/jruby/wiki/CallingJavaFromJRuby#from-class-files
There is mention of the java_import method. Does that help?

"Cannot load Java class" in JRuby

I want to integrate a very simple Java file into my JRuby app. Unfortunately, it doesn't work.
Simple.java:
package com.mypackage;
public class Simple {
public void foo() {
System.out.println("foo called");
}
}
I compile it with "javac Simple.java"
Then I create a jar file with "jar cf mylib.jar Simple.class"
test.rb:
require 'java'
require "mylib.jar"
java_import 'com.mypackage.Simple'
When I run it with "ruby test.rb" I get the following error:
NameError: cannot load Java class com.mypackage.Simple
for_name at org/jruby/javasupport/JavaClass.java:1286
get_proxy_class at org/jruby/javasupport/JavaUtilities.java:34
I have no idea what I'm doing wrong.
My JRuby version is: jruby 1.7.16.1 (1.9.3p392)
The answer is simple. I have forgotten to put the Simple class into the folder structure com/mypackage/Simple. Java was therefore not able to find the class.

Using JUnit in Jython - NameError for assertTrue

Environment Details
Mac OS X 10.9
Oracle JDK 1.7.0_55 64-bit
jython-standalone-2.5.3.jar
junit-4.11
What I have done so far
I have added the junit jar to /Library/Java/Extensions.
I invoked Jython as follows java -jar jython-standalone-2.5.3.jar
In the Jython interpreter, I imported the following import org.junit.Assert, and this import was successful.
Problem
When I tried to use assertTrue, I got a NameError in the interpreter. Why is this so?
I understand that assertTrue is a static method. Not sure what implication this has when I try to use it in Jython.
Additional Context
I am using XMLUnit in Jython. Was able to successfully import the Diff class from org.custommonkey.xmlunit in Jython. Also able to use the methods in this class, and call them on a Diff object. The result of this method call is what I am trying to pass to assertTrue, when it throws the error.
from org.custommonkey.xmlunit import Diff
import org.junit.Assert
xml1 = ...some XML string...
xml2 = ...some XML string...
myDiff = Diff(xml1, xml2)
assertTrue(myDiff.similar())
Hope this additional information is useful in identifying a solution to this problem.
Latest Status
I narrowed it down to setting this property python.security.respectJavaAccessibility = false, since the Assert() constructor is protected.
Still trying to get it to work. Any help is greatly appreciated.
Figured it out.
In addition to junit.jar file, the hamcrest-core.jar file also needed to be copied to /Library/Java/Extensions.
Then I got rid of the jython.jar file, and instead installed it using the jython installer.
After the installation was completed, I updated the registry file in the installation folder, specifically setting this property python.security.respectJavaAccessibility = false.
Now I am able to see the assertTrue method, and no longer getting a NameError.

problems while generating java code from jruby

I have problems while generating .java files with jruby 1.7.3. Here is an example:
class Duck
def quack()
puts "quack!";
end
end
def quack_it(duck)
duck.quack
end
a = Duck.new
quack_it(a)
when I execute
jrubyc --java Test.rb I get the following compilation error:
Failure during compilation of file DuckExample_simple.rb:
undefined method `new_method' for nil:NilClass.
Therefore, I have 2 questions:
What is wrong here?
I want to generate .java files in order to see how the JRuby code is translated into the bytecode and instead of reading the bytecode itself I thought to read the java code. Does the generated java code correspond 1 to 1 to the bytecode generated by AOT jruby compiler, or it's better to read the bytecode itself? I actually want to see how jruby handles dynamic method dispatch at the bytecode level. Any hints would be appreciated.
i don't use jruby so i am not really the best guy to talk to, but here are my 2 cents anyways.
if you just put a simple class into the file, it will work. so try
class Duck
def quack()
puts "quack!"
end
end
it will create a Duck.java file as you would expect, which answeres the second question you had. there is also a nice writeup about the generated file here: http://rhnh.net/2012/10/20/guice-in-your-jruby
i guess that the command is somewhat broken. it would be best to open an issue at the jruby issue tracker: http://jira.codehaus.org/browse/JRUBY

JRuby - include Java vs require "java" - what's the difference?

what's the difference between include Java and require "java"?
require 'java' is called to make all the Java integration facilities available to the script. The require keyword is somewhat similar to the import keyword in Java; it is a signal to load resources from a file whose name follows the keyword
include Java : We include Java API to JRuby.
go through this link