Maniuplating the Java classpath from by JRuby program

Hi All,

This is almost certainly something I’m doing wrong, but I find myself
unable
to successfully modify the Java Classpath from JRuby code. The context
is
‘rake’, in which I want to add some things to the Java classpath, then
run
some JRuby unit tests which require these.

Apparently, by Google-fu is weak. On the basis of things I did find,
I
tried:

  • Modifying the global $CLASSPATH
  • Adding to “$:” (I found a page describing the unification of the Ruby
    and
    Java loadpaths).
  • Modifying ENV[‘CLASSPATH’]

… all to no avail. I have also read that I can ‘require foo.jar’ to
add a
jar file, but in this case it’s a directory of classfiles I’d like to
add.
Can anyone point me to something that should work?

Thanks
–Bob

Hi Bob

Am 07.10.2010 um 18:16 schrieb Bob W.:

This is almost certainly something I’m doing wrong, but I find myself unable to successfully modify the Java Classpath from JRuby code. The context is ‘rake’, in which I want to add some things to the Java classpath, then run some JRuby unit tests which require these.

Apparently, by Google-fu is weak. On the basis of things I did find, I tried:

  • Modifying the global $CLASSPATH
  • Adding to “$:” (I found a page describing the unification of the Ruby and Java loadpaths).
  • Modifying ENV[‘CLASSPATH’]

… all to no avail. I have also read that I can ‘require foo.jar’ to add a jar file, but in this case it’s a directory of classfiles I’d like to add. Can anyone point me to something that should work?

Look at the following example:

require ‘java’

begin
java_import ‘org.apache.commons.io.CopyUtils’
rescue NameError => e
puts “It fails…”
puts e.inspect
end

$CLASSPATH << “target/dependency/commons-io-1.3.2.jar”

java_import ‘org.apache.commons.io.CopyUtils’

Adding a class folder should be the same.

Cheers,
Reto

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

You can also use the JRuby class loader directly if you don’t know the
classes or directories to load, for instance:

JRuby.runtime.jruby_class_loader.addURL(java.io.File.new(DIR_OR_CLASS_PATH).to_url)

2010/10/8 Reto Schüttel [email protected]

Thanks, all for the help.

I indicated originally, that I’d tried adding to $CLASSPATH. I did, but
apparently I did something else wrong. It’s working fine for me now,
after
everyone’s fine advise. Thanks again.

Cheers,
–Bob

  • Modifying the global $CLASSPATH

Can anyone point me to something that should work?

http://kenai.com/projects/jruby/pages/CallingJavaFromJRuby

search for “$CLASSPATH”
GL!
-r

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs