java 调用命令行执行ruby ruby文件必须放在项目跟目录

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class CMD_RUBY {
private static String exeCmd(String commandStr) {
BufferedReader br = null;
try {
Process p = Runtime.getRuntime().exec(commandStr);
br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
//System.out.println(sb.toString());
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
return "error";
}
finally
{
if (br != null)
{
try {
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

public static String post(String data) {
String commandStr = "ruby post_data.rb \""+data+"\"";
//String commandStr = "ipconfig";
return CMD_RUBY.exeCmd(commandStr);
//return "123";
}
}

 

转码相关 

ret = new String(ret.getBytes("gbk"), "utf-8");
System.out.println("返回值"+ret);//打印

posted @ 2016-05-26 17:20  耀yao  阅读(1341)  评论(0编辑  收藏  举报