1

I use jrubyc to compile Ruby into class files. One of the Ruby files contains

dat = File.open "data.json", "r"

And there's a "data.json" file alongside. This program runs well if I directly use the jruby command.

After I compiled the Ruby files and put them into a jar, the following error appears when I run java -jar:

Exception in thread "main" org.jruby.exceptions.RaiseException: (Errno::ENOENT) data.json
at org.jruby.RubyFile.initialize(org/jruby/RubyFile.java:334)
at org.jruby.RubyIO.open(org/jruby/RubyIO.java:1144)
at RUBY.(root)(file:/Users/x5lai/Documents/rqrcode.jar!/read.rb:2)
...

To make sure that I have not put data.json in the wrong place, I have copied data.json all over the jar file, but the same error occurs.

Is there anyway to do this? Is JRuby unable to open JSON files once I have compiled the code?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Erencie
  • 391
  • 2
  • 3
  • 12

1 Answers1

0

I don't think that it is going to look inside the JAR by default. I created a small test and was able to reproduce your issue. I then did touch data.json and the code no longer had an error. I'm not sure how to specify that you want to look inside the JAR for your data file.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • `touch data.json` is not a JRuby method...But one thing you said is correct that it is not going to look inside the JAR by default. I put the data.json **outside** the JAR file and it runs fine. Thanks! – Erencie Nov 11 '13 at 19:48
  • I apologize for the confusion, I meant that I ran `touch data.json` on the command line. `touch` creates an empty file with the given name. Glad you got it figured out! – Shepmaster Nov 14 '13 at 00:35