6

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?

Edu
  • 1,949
  • 1
  • 16
  • 18
  • Thanks very much for this question. I've been searching for how to do this: `jruby -S jrubyc script.rb`, but all I could find was the ruby2java page (http://kenai.com/projects/ruby2java/pages/Home) which means having to use a class in Ruby, and even then I got these two errors **1** http://kenai.com/jira/browse/JVMSCRIPT-7 **2** http://kenai.com/projects/ruby2java/lists/issues/archive/2009-09/message/0 – atomicules Jan 21 '10 at 14:51

1 Answers1

10

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.

Robert Brown
  • 10,888
  • 7
  • 34
  • 40
  • 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 Dec 18 '09 at 11:14