Forum: JRuby rb script not running

Posted by Charles Read (charlesread)
on 2012-11-19 21:58
Hi everybody,

I am very new to JRuby - but it seems awesome - I am able to do a "hello
world" just fine.  My rb script is named 'test.rb' - it runs fine when I
issue the command 'jruby ./test.rb' - and it runs fine when I run
'/usr/local/jruby-1.7.0/bin/jruby test.rb' - but when I run './test.rb'
I get the following errors:

./test.rb: line 4: require: command not found
./test.rb: line 5: JFrame: command not found
./test.rb: line 6: JOptionPane: command not found
./test.rb: line 7: frame: command not found
./test.rb: line 8: syntax error near unexpected token
`JFrame::EXIT_ON_CLOSE'
./test.rb: line 8:
`frame.setDefaultCloseOperation(JFrame::EXIT_ON_CLOSE)  '

I do have the script appropriately hash-banged, I can't figure it out -
can anybody point me in the right direction please?  My 'test.rb' is
below.

Thanks a lot!

<TEST.RB>

#!/usr/local/jruby-1.7.0/bin/jruby
#Ruby running in the JVM

require 'java'
JFrame = javax.swing.JFrame
JOptionPane = javax.swing.JOptionPane
frame  = JFrame.new
frame.setDefaultCloseOperation(JFrame::EXIT_ON_CLOSE)
frame.pack

def showMessage
  return "This is output from a Ruby function!"
end

JOptionPane.showMessageDialog(frame, showMessage);

</TEST.RB>
Posted by Keith B. (keith_b)
on 2012-11-20 00:13
(Received via mailing list)
Charles -

It looks like you may have a blank line before the #! line.  I think
that has to be the very first line, even if there is no executable
code before it.

If not, I'm still pretty sure it's your #! line, and has nothing to do
with your ruby script.  (The errors you're seeing are consistent with
the shell thinking that it should interpret the Ruby code itself.) I'd
suggest commenting out all the Ruby stuff and put something simple
there like puts "Hello", and trying different things with the #!.

Good luck,
- Keith
Posted by Charles Read (charlesread)
on 2012-11-20 01:14
Thanks for the response!

I don't have an extra line above the hash-bang, I just wrote it that way
in my original post.  It's the weirdest thing, I feel like I'm taking
crazy pills!  I attached a screenshot if it helps any (the path of jruby
is a bit different on the machine I am using now).

Thanks again!
Posted by Hassan Schroeder (Guest)
on 2012-11-20 02:18
(Received via mailing list)
On Mon, Nov 19, 2012 at 4:14 PM, Charles Read <lists@ruby-forum.com> 
wrote:

> I don't have an extra line above the hash-bang, I just wrote it that way
> in my original post.

Your script, verbatim other than changing the path in the first line to
match my system's JRuby install, works fine here (JRuby 1.7.0 on
Mac OS).

However, when I first pasted that path into the file, I inadvertently
duplicated the # at the beginning of the line ( ##!/blah/blah/... ). And
that gave me the exact output you're seeing.

Also, perhaps irrelevant, what's that 'jruby.rb' file?

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
http://about.me/hassanschroeder
twitter: @hassan
Posted by Charles Read (charlesread)
on 2012-11-20 02:56
That jruby.rb file is just another "test" script I was playing around 
with.  I'm stumped - I have no other characters or anything floating 
around.  Thank you for your help!
Posted by George Dinwiddie (Guest)
on 2012-11-20 03:28
(Received via mailing list)
Charles,

On 11/19/12 7:14 PM, Charles Read wrote:
> 
http://www.ruby-forum.com/attachment/7885/Screen_S...
What OS are you running? I get similar results on Snow Leopard. I took
out the gui part and just used a printf, and got a shebanged file to
work OK on Linux (headless).

  - George

--
  ----------------------------------------------------------------------
   * George Dinwiddie *                      http://blog.gdinwiddie.com
   Software Development                    http://www.idiacomputing.com
   Consultant and Coach                    http://www.agilemaryland.org
  ----------------------------------------------------------------------
Posted by Charles Read (charlesread)
on 2012-11-20 03:41
I'm running Mountain Lion (10.8.2).

Thanks!
Posted by Keith B. (keith_b)
on 2012-11-20 04:20
(Received via mailing list)
Charles -

The JRuby directory you inserted into test.rb in the shebang line is not
the same directory as the one you ran on the command line:

#!/usr/local/jruby-1.7.0/bin/jruby

vs.

/usr/local/jruby/bin/jruby test.rb


Do you have a /usr/local/jruby-1.7.0/bin/ directory?  What happens if
you do this?:

ls -l /usr/local/jruby-1.7.0/bin/jruby


Also, another way to make the script executable is instead of specifying
an absolute directory, you can do this:

#!/usr/bin/env jruby

Can you try that instead and see if it works?

- Keith

Keith R. Bennett
http://about.me/keithrbennett
Posted by Charles Read (charlesread)
on 2012-11-21 00:43
Well, I tried #!/usr/bin/env jruby and it worked just fine - but why 
wouldn't the absolute path work - hmmmm....

And the difference in paths for jruby was just the different locations 
on my work computer and home computer - I started testing it at work and 
then went home, so I had to change paths.

If anybody has any more ideas about why the absolute path isn't working 
I would love to hear them - thanks everybody!
Posted by Kimberley Scott (Guest)
on 2012-11-21 00:56
(Received via mailing list)
I kinda had a similar problem on OS X.
Turned out that the jruby script itself has "env bash" shebang.
Which seemed to muck up the paths due to OS X bash config.
My fix was to change the "#!/opt/jruby-1.7.0/bin/jruby" shebang at the 
top of the ../bin/rails, rake etc with:
#!/usr/bin/env jruby
#!/opt/jruby-1.7.0/bin/jruby (for reference)
I wrote a short incomplete post about it here:

http://www.randomactsofsentience.com/2012/10/os-x-...

Might help.

Kimbo

On 21/11/2012, at 9:43 AM, Charles Read wrote:

> --
> Posted via http://www.ruby-forum.com/.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>

Ms Kimberley Scott
Senior Software Engineer
http://www.randomactsofsentience.com/
http://www.facebook.com/kimberley.scott.14
Posted by Charles Read (charlesread)
on 2012-11-21 00:59
I'll give that a shot - thanks!
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.