How to Import All Packages in Jruby

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:

  1. use Ruby Module Syntax:

    Java::Foo::Foo.main()

  2. 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"


  1. 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:

  1. You've misspelled "practice" in irb: java_import 'com.pratice.Test'
  2. Your class isn't actually in the correct package. You need to add package com.practice; to the Java code.
  3. 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



Leave a reply



Submit