Hi,
I've never used JRuby before.
Today, I wanted to see how easy|difficult it is to run a (J)Ruby script which
uses Apache Jena from Java.
Pretty easy.
---- Java:
ScriptEngineManager sem = new ScriptEngineManager();
ScriptEngine ruby = sem.getEngineByExtension("rb");
try {
FileReader reader = new FileReader(new File("src/main/jruby",
"hello_jena.rb"));
ruby.eval(reader);
} catch (ScriptException ex) {
ex.printStackTrace();
}
----
https://github.com/castagna/jena-examples/blob/master/src/main/java/org/apache/jena/examples/ExampleJRuby_03.java
---- Ruby:
require 'java'
java_import java.lang.System
java_import org.apache.jena.examples.Utils
java_import com.hp.hpl.jena.rdf.model.ModelFactory
input = Utils.getResourceAsStream("data/data.ttl")
model = ModelFactory.createDefaultModel()
model.read(input, nil, "TURTLE")
puts "\n---- Turtle ----"
model.write(System.out, "TURTLE")
----
https://github.com/castagna/jena-examples/blob/master/src/main/jruby/hello_jena.rb
The only thing I needed to add to my pom.xml is a dependency on jruby-core:
<dependency>
<groupId>org.jruby</groupId>
<artifactId>jruby-core</artifactId>
<version>1.6.5</version>
</dependency>
I also installed JRuby and I can run:
jruby -v
jruby 1.6.5 (ruby-1.8.7-p330) (2011-10-25 9dcd388) (Java HotSpot(TM) 64-Bit
Server VM 1.6.0_26) [linux-amd64-java]
Or, a simple Ruby script:
jruby src/main/ruby/hello_world.rb
Hello World!
However, when I try to run a (J)Ruby script which uses Apache Jena I get an
error:
jruby src/main/jruby/hello_jena.rb
NameError: cannot load Java class org.apache.jena.examples.Utils [...]
Off course, I did not setup the Java/JRuby classpath.
This is where my good first experience with JRuby is becoming a bad one.
How can I setup the classpath for the jruby command?
The only way I found so far is this:
mvn assembly:assembly -DdescriptorId=jar-with-dependencies
export CLASSPATH=target/jena-examples-0.1-SNAPSHOT-jar-with-dependencies.jar
jruby src/main/jruby/hello_jena.rb
If you use JRuby (with Apache Jena), is this what you normally do to run your
(J)Ruby scripts from the command line?
Cheers,
Paolo