java 调用命令行执行ruby ruby文件必须放在项目跟目录
Posted 耀yao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);//打印
以上是关于java 调用命令行执行ruby ruby文件必须放在项目跟目录的主要内容,如果未能解决你的问题,请参考以下文章