"require" non-packed Java package

Hello everybody,

What is a way to “require” Java package that is not packed into JAR?

From my point of view I could expect something like follow to work:

include Java
require ‘/mypath/project1/bin’
java_import ‘com.my.package.MyClass’

where project1 is something built by Eclipse or NetBeans, without
generating project1.jar each time.
Unfortunately, my guess is wrong. At least with JRuby 1.1.5.

Thanks,
Arkadi.

You can’t add a directory to the classpath via require(), you have to
instead do something akin to

JRuby.runtime.jruby_class_loader.addURL( … )

where the URL you add points to a directory (or a JAR).

-Bob

On Jul 29, 2010, at 9:09 AM, Arkadi K. wrote:

where project1 is something built by Eclipse or NetBeans, without

http://xircles.codehaus.org/manage_email


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

On 29 Jul 2010, at 14:11, Bob McWhirter wrote:

You can’t add a directory to the classpath via require(), you have to instead do something akin to

JRuby.runtime.jruby_class_loader.addURL( … )

Try:

$CLASSPATH << ‘/path/to/my/classes’

irb(main):001:0> require ‘java’
=> true
irb(main):002:0> $CLASSPATH
=> []
irb(main):003:0> require ‘saxon9.jar’
=> true
irb(main):004:0> $CLASSPATH
=> [“file:/Users/pldms/Library/Java/Saxon/saxon9.jar”]
irb(main):005:0> $CLASSPATH << ‘classes’
=> [“file:/Users/pldms/Library/Java/Saxon/saxon9.jar”,
“file:/Users/pldms/Library/Java/Saxon/classes/”]

$CLASSPATH is magic.

Damian


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

JRuby.runtime.jruby_class_loader.addURL( … )
Seems good enough for me.

However, the follow error is printed:
(eval):1:in include_class': cannot load Java class com.my.pkg.MyClass (NameError) from file:/C:/JRuby/jruby-1.1.5/lib/jruby.jar!/builtin/javasupport/core_ext/object.rb:38:ineval’
from
file:/C:/JRuby/jruby-1.1.5/lib/jruby.jar!/builtin/javasupport/core_ext/object.rb:67:in
include_class' from file:/C:/JRuby/jruby-1.1.5/lib/jruby.jar!/builtin/javasupport/core_ext/object.rb:38:ineach’
from
file:/C:/JRuby/jruby-1.1.5/lib/jruby.jar!/builtin/javasupport/core_ext/object.rb:38:in
include_class' from file:/C:/JRuby/jruby-1.1.5/lib/jruby.jar!/builtin/javasupport/core_ext/object.rb:81:injava_import’
from r1.rb:4

“require ‘project1.jar’” works fine with the same sources.
The sample sources are attached.

Thanks,
Arkadi.

$CLASSPATH << ‘/path/to/my/classes’
The $CLASSPATH << ‘/path’ part works, however the error remains the
same:
“(eval):1:in `include_class’: cannot load Java class com.my.pkg.MyClass”
and points into “java_import ‘com.my.pkg.MyClass’”.

After some extensive testing, the follow syntax works:
JRuby.runtime.getJRubyClassLoader.addURL(Java::java.net.URL.new(path))
java_import ‘com.my.pkg.MyClass’

However, there is an odd restriction on what “path” can be:
if it`s not JAR, it must end with “/”.
This restriction confuses, specially after reviewing Sun specification:
http://download.oracle.com/javase/6/docs/technotes/tools/windows/classpath.html

Thanks again to Bob.

I had run into that long ago and added it to the Wiki FAQ. Finding it
is another thing…

http://kenai.com/projects/jruby/pages/FAQs#How_come_Java_can_t_find_resources_in_class_folders_that_I_ve_appended_to_the_$CLASSPATH_global_variable_at_runtime
?

To summarize, it’s all related to Java’s URLClassLoader

This class loader is used to load classes and resources from a search
path of URLs referring to both JAR files and directories. Any URL that
ends with a ‘/’ is assumed to refer to a directory. Otherwise, the URL
is assumed to refer to a JAR file which will be opened as needed.

-lenny

Yeah, I hate that duality. Sometimes you need the /, sometimes you
don’t. Very confusing.

Glad you got it working. You should add something to the wiki (maybe a
FAQ, or find somewhere that talks about loading Java libs) here:

On Sun, Aug 1, 2010 at 3:06 AM, Arkadi K. [email protected]
wrote:


Posted via http://www.ruby-forum.com/.


To unsubscribe from this list, please visit:

  http://xircles.codehaus.org/manage_email


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email