tags:

views:

472

answers:

1

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?

+5  A: 

Hi Edu,

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.

Rob
Works perfectly! Thank you Rob. I knew it was some sort of small detail that I was missing cause I followed all the steps.
Edu