开发者

Why can't I call javac using the Backquotes/Backticks approach in Ruby?

开发者 https://www.devze.com 2022-12-12 14:46 出处:网络
I am trying to compile a java source file via a Ruby Script. However I am a bit puzzled by the following behavior

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# 2开发者_开发技巧005 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.


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).


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}"


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.


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.


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.
0

精彩评论

暂无评论...
验证码 换一张
取 消