How can I import all packages in jruby?
require 'java'
module JavaUtil
include_package "java.util"
end
JavaUtil::Date.new
Importing all classes in java package in jruby
require 'java'
module JavaUtil
include_package "java.util"
end
JavaUtil::Date.new
Can I import Java class files into JRuby?
I've managed to answer my own question :)
If your class files are compiled to a relative path of "target", e.g. foo.Bar is located in "target/foo/Bar.class", then you do the following:
require 'java'
require 'target/foo/Bar'
module Foo
include_package 'foo'
end
puts Foo::Bar.new
And the result:
foo.Bar@1582a7c
Importing .jar file in Ruby: how to use the class
In jruby, you can access java classes by package name and class name.
See https://github.com/jruby/jruby/wiki/CallingJavaFromJRuby
Your java main method is static, so no need for new
. So this should work.
jruby.Jruby.main()
But I suggest to rename your java class and package. You may get name clashes, as it is likely that something inside jruby is also called jruby
.
Also the name is just wrong, because it is a java class and not a jruby class.
Edit:
After consulting the document, the above is only correct for standard java packages. There are several ways to access a java class:
use Ruby Module Syntax:
Java::Foo::Foo.main()
the dot syntax, as I tried first.
"Second way: for the top-level Java packages java, javax, javafx, org,
and com you can type in a fully qualified class name basically as in
Java, for example, java.lang.System or org.abc.def.className ..."
You either need to put your java class in the package com.foo
or write an extra method to access the package:
def edu
Java::Edu
end
"And then you can use use usual Java package names like
edu.abc.def.ClassName"
after java_import, the imported Class "will be available in the global name space"
java_import 'foo.Foo'
Foo.main()
In all cases, you need to make sure that jruby sees the your jar file.
" loading jar-files via require searches along the $LOAD_PATH for
them, like it would for normal ruby files."
JRuby: import vs include vs java_import vs include_class
You can find quite a few examples about working with Java classes at:
https://github.com/jruby/jruby/wiki/CallingJavaFromJRuby
It states, that you should use java_import
instead of import
due to the JRUBY-3171 bug.
Also include_class
is or will be deprecated (JRUBY-3797) in favor of java_import
.
Currently java_import
is the recommended way to import a Java class.
How do i load a compiled Java class in JRuby?
Still upvoting, but found it out myself, i just need to do
Java::dice.Dice
because, it only works without the Java::
part when it is in the java folder of packages like
java.lang.System
import java class in JRuby
You have a number of problems:
- You've misspelled "practice" in irb:
java_import 'com.pratice.Test'
Your class isn't actually in the correct package. You need to addpackage com.practice;
to the Java code.- You need to add "bin" to your classpath so that the JVM can find the classes:
$CLASSPATH << 'bin'
Altogether, I was able to run this in IRB:
$CLASSPATH << 'bin'
com.practice.Test.foo
# => "Java!!"
EDIT: I failed to copy your package
line, that's my fault, not yours. :-)
Can I use wildcards in my java_import statements to load into the global namespace?
The answer appears to be "no". You can't load all of a package's classes into the global namespace. At least, not easily.
A post here appears to describe a way, though it is incredibly ugly. (I got that link from the JRuby Github wiki).
That ugly way looks something like this, but it did not work for me.
module M
include_package "com.example.shapes"
end
class Object
class << self
alias :const_missing_old :const_missing
def const_missing c
M.const_get c
end
end
end
Circle #should work
Triangle #should work
Again, it didn't work for me, but I might have messed something up. I'm not going to pursue, because I'm not really interested in putting crazy hacks like this in my code.
Related Topics
Automatically Log Android Lifecycle Events Using Activitylifecyclecallbacks
Onintercepttouchevent Only Gets Action_Down
Center Message in Android Dialog Box
Listview Selection Remains Persistent After Exiting Choice Mode
How to Use Tablayout with Viewpager2 in Android
App Crashing When Trying to Use Recyclerview on Android 5.0
Android Studio - Failed to Complete Gradle Execution - Error in Opening Zip File
How to Detect a Click in an Ontouch Listener
Convert a String to a Byte Array and Then Back to the Original String
Xmpp with Java Asmack Library Supporting X-Facebook-Platform
Google Gson Linkedtreemap Class Cast to Myclass
"Hello World" Android App with as Few Files as Possible, No Ide, and Text Editor Only
Convert Normal Java Array or Arraylist to JSON Array in Android
Fragment Add or Replace Not Working
How to Change the Edittext Text Without Triggering the Text Watcher
Polygon Touch Detection Google Map API V2