问题描述
我已经用 Ruby 编程一段时间了,我真的很喜欢它.最近我开始需要编译一些 ruby 代码.出于多种原因,使用 Ruby2exe 不是我的选择.所以我决定尝试一下 Jruby(生成一个 jar 就足够了).
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).
我使用的是 Windows 并且我安装了 java JDK 6u17(位于 C:Program FilesJavajdk1.6.0_17).
I am using windows and I installed java JDK 6u17 (at C:Program FilesJavajdk1.6.0_17).
我在 C:jruby 安装了 jruby 1.4
I installed jruby 1.4 at C:jruby
我用 java 创建了一个 hello world,编译并执行它就好了(所以 java 工作正常).
I created a hello world in java, compile and executed it just fine (so java works fine).
我创建了一个文件script.rb":
I created a file "script.rb" with:
输入你好,世界"
我用 jruby 运行这个程序:
I run this program with jruby:
jruby script.rb
而且效果很好.
我确实将 JAVA_HOME 设置为 C:Program FilesJavajdk1.6.0_17
I did set JAVA_HOME to C:Program FilesJavajdk1.6.0_17
我也成功运行了:
java -jar c:jrubylibjruby.jar script.rb
然后我用命令编译:
jruby -S jrubyc script.rb
它生成类'script.class'
It generates the class 'script.class'
我的问题是我找不到正确执行 script.class 的方法
My problem is that I found no way to properly execute script.class
我尝试:
java -cp .:c:jrubylibjruby.jar 脚本
然后我收到错误消息:
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.
我还尝试将 jruby-complete-1.4.0.jar 以及其他几个选项复制到本地目录.
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?
推荐答案
假设你在 windows 上,我认为你的 -cp arg 是错误的:它应该用分号分隔:
Assuming you are on windows, I think your -cp arg is wrong: it should be semi-colon delimited:
java -cp .;c:jrubylibjruby.jar 脚本
而且,我通过单独设置 CLASSPATH 环境获得了更好的运气,例如:
But also, I had better luck by setting the CLASSPATH env separately, e.g.:
C:
uby>set CLASSPATH=c:Program Filesjruby-1.4.0libjruby.jar;
C:
uby>java hello_world
Hello, world!
但也许那是因为我的类路径中需要一个空格.
But perhaps that's because my classpath needs a space in it.
您使用的是哪个版本的 JRuby?如您所见,我使用的是 1.4.
What version of JRuby are you using? As you can see, I'm on 1.4.
这篇关于编译 jruby “Hello world"问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!