问题
How to call jar
file (a java archive file) from ruby
class. And access the functions/methods from it.
I am refering the following link
But I am getting the following error:
'require': cannot load such file -- /tmp/Test.jar (LoadError)
How can I resolve this issue.
回答1:
You can use a system call. You may have performance issues if you have lots of users, but for most this should work:
# Create a directory (in this sample called "jars") inside public directory and move your jar file inside the newly created directory.
def externaljar
argsA = ""
argsB = ""
Dir.chdir("#{RAILS_ROOT}/public/jars/") do
# Use your path to the java according to the java directory installed in your machine.
retResult = system("/home/user/java/jdk1.7.0/bin/java -jar JSpell.jar #{argsA} #{argsB}")
end #chdir
end
回答2:
You can't call Java code from Ruby. You might want to have a look at JRuby. JRuby is what the guy from your link uses, you can see it when he types jruby Main.rb
in the console.
来源:https://stackoverflow.com/questions/15174068/call-jar-file-from-ruby-class