java - Access classes defined in a jar file, from within (for instance) JRuby irb? -


(crossposting note: have asked question 1 week ago @ jruby mailing list, didn't answer yet).

i have jar file provided else, no access source code. jar file in lib/other/appl.jar, class named appl, , package com.abc.xyz

i instantiate appl object jruby irb, jirb_swing_ex.

(of course problem applies not jirb, running jruby programs in general, explain in way i'm using right now, in case there peculiarities in jirb need special treatment).

this way work:

(1) invoke jirb by:

java -jar jr/jruby-complete-1.7.27 jb/jirb_swing_ex 

(2) put directory jar file load path:

$: << 'lib/other' 

(3) load jar file

require 'appl.jar' 

(4) import class

java_import com.abc.xyz.appl 

(5) create object

x = appl.new 

as said, works, , can live if necessary, prefer simpler approach:

now question: instead of fiddling around load path , doing require jar file, thought let java include jar file. have tried:

java -cp lib/other/appl.jar -jar jr/jruby-complete-1.7.27 jb/jirb_swing_ex 

the problem is: how can @ object? if use class name com.abc.xyz.appl, jruby complains class not found (nameerror: missing class name).

btw, have tried forward slashes (since i'm on windows), i.e.

java -cp lib\other\appl.jar -jar jr\jruby-complete-1.7.27 jb\jirb_swing_ex 

but same effect. had expected that, having appl.jar in class path, make classes available somehow, seem miss something.

running jirb or jirb_swing custom class path

jirb , jirb_swing use value of jruby_cp environment variable (if present) extend class path given java command line.

example commons-lang3 library taken local maven repository, using bash on linux or macos:

$ export jruby_cp=${home}/.m2/repository/org/apache/commons/commons-lang3/3.4/commons-lang3-3.4.jar  $ jirb irb(main):001:0> org.apache.commons.lang3.mutable.mutableboolean.new => #<java::orgapachecommonslang3mutable::mutableboolean:0x7c24b813> 

running jruby programs custom class path

to run jruby program uses third-party java library, won't work:

java -cp lib/other/appl.jar -jar jr/jruby-complete-1.7.27 ... 

you must use either -jar or -cp, can't combine two.
java man page:

when use option [-jar], jar file source of user classes, , other user class path settings ignored.

in addition, need pass main java class, org.jruby.main, , class needs arguments: either path ruby script, or other command line arguments such -e 'puts 2+2'.

so command line structure following:

# run script file: java -cp path/to/jruby.jar:other/custom.jar org.jruby.main path/to/script # run simple one-line ruby program java -cp path/to/jruby.jar:other/custom.jar org.jruby.main -e 'some ruby here' 

(on windows please use ; instead of : separator)

actual example same commons-lang3 library & os:

$ alias myjruby="java -cp ${jruby_home}/lib/jruby.jar:${home}/.m2/repository/org/apache/commons/commons-lang3/3.4/commons-lang3-3.4.jar org.jruby.main"  # verifying base jruby code works that: $ myjruby -e 'puts 2+2' 4  # verifying can use 3rd-party lib: $ myjruby -e 'puts org.apache.commons.lang3.mutable.mutableboolean.new' false 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -