I have been programming for a while with Ruby and I really enjoy it. Lately I started having the need of compiling some ruby code. For several reasons using Ruby2exe is not an option for me. So I decided to give Jruby a try (generating a jar would be good enough).
I am using windows and I installed java JDK 6u17 (at C:\Program Files\Java\jdk1.6.0_17).
I installed jruby 1.4 at C:\jruby
I created a hello world in java, compile and executed it just fine (so java works fine).
I created a file "script.rb" with:
puts "Hello, world"
I run this program with jruby:
jruby script.rb
And it works fine.
I did set JAVA_HOME to C:\Program Files\Java\jdk1.6.0_17
I also successfully run:
java -jar c:\jruby\lib\jruby.jar script.rb
I then compile with the command:
jruby -S jrubyc script.rb
It generates the class 'script.class'
My problem is that I found no way to properly execute script.class
I try:
java -cp .:c:\jruby\lib\jruby.jar script
And I get the error message:
Exception in thread "main" java.lang.NoClassDefFoundError: script
Caused by: java.lang.ClassNotFoundException: script
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: script. Program will exit.
I also tried copying jruby-complete-1.4.0.jar to local dir as well as several other options.
Anyone know what am I doing wrong?
Unpack JRuby: Unpack the file you downloaded. You'll then have a "jruby-<version>" directory. Run JRuby: The JRuby startup script lives in the "bin" directory. Run "bin/jruby -v" to confirm it.
JRuby is designed to work as a mixed-mode virtual machine for Ruby, where code can be either interpreted directly, just-in-time compiled at runtime to Java bytecode, or ahead-of-time compiled to Java bytecode before execution.
Using JRuby allows you to blur one of the starker distinctions among programming languages, that between interpreted languages and compiled languages. Interpreted languages, such as Ruby, Python, or JavaScript, are executed by an interpreter in a line-by-line fashion.
Assuming you are on windows, I think your -cp arg is wrong: it should be semi-colon delimited:
java -cp .;c:\jruby\lib\jruby.jar script
But also, I had better luck by setting the CLASSPATH env separately, e.g.:
C:\ruby>set CLASSPATH=c:\Program Files\jruby-1.4.0\lib\jruby.jar;
C:\ruby>java hello_world
Hello, world!
But perhaps that's because my classpath needs a space in it.
What version of JRuby are you using? As you can see, I'm on 1.4.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With