Ruby学习十 JRuby中调用java代码
?
require 'java'
?
# 以下请注意要用相对路径
require 'C:/jruby-1.6.2/lib/opencsv-2.2.jar'
require 'C:/jruby-1.6.2/lib/UncompressInputStream.jar'
?
class UnCompress
? def get_input_stream(file)
??? input = java.io.FileInputStream.new(file);
??? if /\.gz$/ =~ file
????? input = java.util.zip.GZIPInputStream.new(input);
??? elsif? /\.Z$/ =~ file
????? input = Java::org.amanzi.uncompress.UncompressInputStream.new(input);
??? end
??? input
? end
? def un_compress_file(file)
??? p "start."
??? input_stream = get_input_stream(file)
??? character_set = java.lang.String.new("UTF-8")
??? reader = Java::au.com.bytecode.opencsv.CSVReader.new(java.io.InputStreamReader.new(input_stream,character_set))
??? all_line = []
??? while (next_line = reader.readNext)
????? all_line << next_line[0]
??? end
??? all_line
??? p all_line
? end
end
?
2、调用java 方法时:
Java::包路径.方法名称
Java::com.dingli.test.TestJrubyCallJava()
?
3、java 打成 jar 包:
?
A. 先生成.class 文件.
B.源文件(.java) 需要带有包路径 eg:package com.dingli.test;
C.jar cvfm TestJrubyCallJava.jar mymanifest -C class/ .
?
D.class 的目录结构为:class/com/dingli/test/TestJrubyCallJava.class
?
这样打出来的jar 是带用包路径的。
?
?
?
?