JRuby

We should have a $CLASSPATH variable

Details

  • Type: New Feature New Feature
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: None
  • Fix Version/s: JRuby 1.0.0RC2
  • Component/s: Java Integration
  • Labels:
    None
  • Number of attachments :
    0

Description

To interact with Java integration in a nice way, there should be a $CLASSPATH special global. This should be an object that defines each, << and includes Enumerable. Since we cannot change the $CLASSPATH list, there shouldn't be any other Array operations on it. The << operation should take File, URL and String and handle most cases "intelligently". It probably should be loaded at the same time as the Java module.

Issue Links

Activity

Hide
Charles Oliver Nutter added a comment -

This seems doable for 1.0.

Show
Charles Oliver Nutter added a comment - This seems doable for 1.0.
Hide
Charles Oliver Nutter added a comment -

Moving to post RC, since the capabilities are there but we haven't added this. It's a pretty low priority for us at the moment though, so if someone really wants this they probably ought to implement it themselves.

Show
Charles Oliver Nutter added a comment - Moving to post RC, since the capabilities are there but we haven't added this. It's a pretty low priority for us at the moment though, so if someone really wants this they probably ought to implement it themselves.
Hide
Ola Bini added a comment -

Added, since it was very easy to do.

Show
Ola Bini added a comment - Added, since it was very easy to do.
Hide
Antti Karanta added a comment -

Where could I find an example of how to use this from (J)Ruby code? I.e. how to add jars to load path and then load a java class from therein.

This is not currently documented in distribution's doc/Javasupport-overview.html, doc/Javasupport-lowlevel.html or doc/Javasupport-highlevel.html. It should be there as it is fairly important (IMO).

I tried adding to $CLASSPATH with << but it did not work, I kept getting NameError:

:-1:in `eval': cannot load Java class fi.napa.Napa (NameError)
from file:/C:/programs/jruby/lib/jruby.jar!/builtin/javasupport/core_ext/object.rb:52:in `include_class'
from :-1:in `each'
from file:/C:/programs/jruby/lib/jruby.jar!/builtin/javasupport/core_ext/object.rb:54:in `include_class'
from :-1

The class fi.napa.Napa is certainly in one of the jars included into $CLASSPATH

Here's the script I used (easy to modify to use any jardir / included class):

require 'java'

jardir = 'd:/work/napa/jnapa/build/'

jars = Dir.entries( jardir ).find_all { |f| f =~ /.jar$/ }

jars.each { |j| $CLASSPATH << "#{jardir}#{j}" }

$CLASSPATH.each {|e| puts e}
puts $CLASSPATH.size

include_class( 'fi.napa.Napa' ) { 'Napa' }

BTW, puts $CLASSPATH prints the entries without any separator and is a little hard to read. It would be nice to have the to_s to join the entries w/ File::PATH_SEPARATOR

Show
Antti Karanta added a comment - Where could I find an example of how to use this from (J)Ruby code? I.e. how to add jars to load path and then load a java class from therein. This is not currently documented in distribution's doc/Javasupport-overview.html, doc/Javasupport-lowlevel.html or doc/Javasupport-highlevel.html. It should be there as it is fairly important (IMO). I tried adding to $CLASSPATH with << but it did not work, I kept getting NameError: :-1:in `eval': cannot load Java class fi.napa.Napa (NameError) from file:/C:/programs/jruby/lib/jruby.jar!/builtin/javasupport/core_ext/object.rb:52:in `include_class' from :-1:in `each' from file:/C:/programs/jruby/lib/jruby.jar!/builtin/javasupport/core_ext/object.rb:54:in `include_class' from :-1 The class fi.napa.Napa is certainly in one of the jars included into $CLASSPATH Here's the script I used (easy to modify to use any jardir / included class): require 'java' jardir = 'd:/work/napa/jnapa/build/' jars = Dir.entries( jardir ).find_all { |f| f =~ /.jar$/ } jars.each { |j| $CLASSPATH << "#{jardir}#{j}" } $CLASSPATH.each {|e| puts e} puts $CLASSPATH.size include_class( 'fi.napa.Napa' ) { 'Napa' } BTW, puts $CLASSPATH prints the entries without any separator and is a little hard to read. It would be nice to have the to_s to join the entries w/ File::PATH_SEPARATOR
Hide
Radoslaw Bulat added a comment -

It doesn't work for me too. I've got simple Ruby script:

require 'java'

$CLASSPATH << "."
include_class 'ListPrinter'

ListPrinter class exists (ListPrinter.class file) in the same directory where Ruby script is. But, when I set $CLASSPATH environment variable to "." value it works.

My environment:

jruby -v
ruby 1.8.5 (2007-06-07 rev 3841) [x86-jruby1.0]

java -version
java version "1.6.0_01"
Java(TM) SE Runtime Environment (build 1.6.0_01-b06)
Java HotSpot(TM) Client VM (build 1.6.0_01-b06, mixed mode, sharing)

system: windows xp
Show
Radoslaw Bulat added a comment - It doesn't work for me too. I've got simple Ruby script:
require 'java'

$CLASSPATH << "."
include_class 'ListPrinter'
ListPrinter class exists (ListPrinter.class file) in the same directory where Ruby script is. But, when I set $CLASSPATH environment variable to "." value it works. My environment:
jruby -v
ruby 1.8.5 (2007-06-07 rev 3841) [x86-jruby1.0]

java -version
java version "1.6.0_01"
Java(TM) SE Runtime Environment (build 1.6.0_01-b06)
Java HotSpot(TM) Client VM (build 1.6.0_01-b06, mixed mode, sharing)

system: windows xp
Hide
Bryan Weber added a comment -

Ok, folks I think I've figured out what is going on here:

**DOES NOT WORK**
require 'java'

$CLASSPATH << "/Path/to/classes"
include_class 'ListPrinter'

**WORKS**
require 'java'

$CLASSPATH << "/Path/to/classes/"
include_class 'ListPrinter'

Notice the trailing slash after the path to the classes in the string. Ideally the java module should be fixed to check for the trailing slash and add it if not present, but this should work for everyone in the mean time.

Show
Bryan Weber added a comment - Ok, folks I think I've figured out what is going on here: **DOES NOT WORK** require 'java' $CLASSPATH << "/Path/to/classes" include_class 'ListPrinter' **WORKS** require 'java' $CLASSPATH << "/Path/to/classes/" include_class 'ListPrinter' Notice the trailing slash after the path to the classes in the string. Ideally the java module should be fixed to check for the trailing slash and add it if not present, but this should work for everyone in the mean time.
Hide
Antti Karanta added a comment -

Your solution does not help when adding jar files to $CLASSPATH, e.g.

$CLASSPATH << 'd:/work/napa/jnapa/build/derby.jar' # adding / to the end does not help

Any workarounds for this?

Show
Antti Karanta added a comment - Your solution does not help when adding jar files to $CLASSPATH, e.g. $CLASSPATH << 'd:/work/napa/jnapa/build/derby.jar' # adding / to the end does not help Any workarounds for this?

People

Vote (0)
Watch (2)

Dates

  • Created:
    Updated:
    Resolved: