tags:

views:

168

answers:

5

I am trying to compile a java source file via a Ruby Script. However I am a bit puzzled by the following behavior

compile_results = `javac  #{source_file}`

this fails to run with a 'No such file...' error. I popped up irb

irb(main):001:0> `javac -help`
Errno::ENOENT: No such file or directory - javac -help
        from (irb):1:in ``'
        from (irb):1

irb(main):002:0> `csc`
=> "Microsoft (R) Visual C# 2005 Compiler version 8.00.50727.3053\nfor Microsoft
 (R) Windows (R) 2005 Framework version 2.0.50727\nCopyright (C) Microsoft Corpo
ration 2001-2005. All rights reserved.\n\nfatal error CS2008: No inputs specifie
d\n"

However both javac and csc are on the PATH. e.g. if i run javac manually from the shell that I run the ruby script from, I am able to get to the java compiler. The source file exists.

I tried both ruby 1.8.7 and 1.9.1 (Windows). Does anyone see something that I am missing ?

Update: I dont think it has to do with command line args. Rather it can't get to javac for some weird reason. I put the line javac %1 in a batch file and call the batch file in the usual way. This worked... but still am not sure of what the whole issue was with javac.

A: 

Here are a couple of possible reasons:

  1. source_file have spaces somewhere in its name or path. Quote argument to javac.
  2. source_file is a relative path, but you cannot get to that relative path from you current working directory.
Alexander Pogrebnyak
No. "-help" doesn't contain a space. And no, if javac is on the path then it's accessible.
Carl Smotricz
A: 

An obvious difference between the two is that you're not running csc with an argument.

Just to get closer to the solution, write a JavaHelp.bat (or .cmd if you prefer), put it on the path and call that from ruby.

Another thing you could try is to call java explicitly as javac.exe . I don't have much hope in that, though, as csc without .exe works too.

Finally, you could try interposing your own shell: Try something like

cmd /c javac -help

(In all this, I'm assuming you're on Windows).

Carl Smotricz
Yes the delegating batch file was what I came up with too.. just before I went to sleep :) And it did work too.. but still not sure of the root cause of this problem.
Gishu
+1  A: 

It seems that Ruby on Windows doesn't like the

`command -with-args`

syntax. You might try

%x[javac -help]

or

%x[javac #{source_file}]

or

system 'javac', '-help'

or

system 'javac', "#{source_file}"
James A. Rosen
didn't work. Same error.
Gishu
A: 

Javac may be in your shell's path but as far as Ruby is concerned in this instance it isn't. Csc clearly is in your path and gives you errors as it should but javac doesn't.

If you just run javac does it give you the same ENOENT? If so it's just a path problem. It's entirely possible that your environment in Ruby is different from that in the shell. Can you print out your environment and make sure that the path is actually correct?

Lastly, I don't know if they still use it, I've not used java in ages, but doesn't java have a different path system? I seem to recall you have to set JAVA_HOME or something instead of the path (or in addition to in case the path fails?).

Been a while since I last used windows, apologies if these are all dead-ends.

Chuck Vose
No, JAVA_HOME isn't needed any more. Some Java programs like to see it but Java (java, javac, javaw) will happily dig out the path to the executable and extrapolate the location of its library from there.
Carl Smotricz
A: 

Do this instead:

`C:/java_location/bin/javac.exe arguments`

And replace the C:/Java_location with the actual location of the JDK. This should work, and you won't need an additional batch file.

Geo