Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile jruby "Hello world" problem

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?

like image 284
Edu Avatar asked Dec 17 '09 12:12

Edu


People also ask

How do I run a script on JRuby?

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.

How does JRuby work?

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.

Why to use JRuby?

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.


1 Answers

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.

like image 59
Robert Brown Avatar answered Sep 19 '22 23:09

Robert Brown