public static void main(String args[]) throws Exception{
Process p1 = null;
String exep1 = "grep -c 'TAG' /home/zhenm/xiaoxiao/run/test";
System.out.println(exep1);
p1=Runtime.getRuntime().exec(exep1); // 等待命令执行完毕
p1.waitFor();
//获取执行结果
InputStream out1= p1.getInputStream();
InputStreamReader in1 = new InputStreamReader(out1);
BufferedReader br1 = new BufferedReader(in1);
String line1=null;
String res1="";
line1=br1.readLine();
while(line1!=null){
res1=res1+line1;
line1=br1.readLine();
}
System.out.println(res1);
我在java中用grep -c命令来检测一个文件中有多少行包含某个字符串,如上代码中:命令grep -c 'TAG' /home/zhenm/xiaoxiao/run/test直接在shell下运行结果是正确的,结果为1,因为test里面有一行包含字符TAG,但是放在java程序里面调用执行,结果总是0,总得不到和shell一致的正确结果。
麻烦高手帮忙,这个问题困扰我好几天了,急急急啊!先谢谢大家~
Process p1 = null;
String exep1 = "grep -c 'TAG' /home/zhenm/xiaoxiao/run/test";
System.out.println(exep1);
p1=Runtime.getRuntime().exec(exep1); // 等待命令执行完毕
p1.waitFor();
//获取执行结果
InputStream out1= p1.getInputStream();
InputStreamReader in1 = new InputStreamReader(out1);
BufferedReader br1 = new BufferedReader(in1);
String line1=null;
String res1="";
line1=br1.readLine();
while(line1!=null){
res1=res1+line1;
line1=br1.readLine();
}
System.out.println(res1);
我在java中用grep -c命令来检测一个文件中有多少行包含某个字符串,如上代码中:命令grep -c 'TAG' /home/zhenm/xiaoxiao/run/test直接在shell下运行结果是正确的,结果为1,因为test里面有一行包含字符TAG,但是放在java程序里面调用执行,结果总是0,总得不到和shell一致的正确结果。
麻烦高手帮忙,这个问题困扰我好几天了,急急急啊!先谢谢大家~
试试
package com.bokecc.test.csdn;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;public class Test{
public static void main(String[] args) {
try {
ProcessBuilder builder = new ProcessBuilder("/bin/bash","-c","grep -c 'TAG' /home/cc/test");
builder.redirectErrorStream(true);
Process p = builder.start();
//Process p = Runtime.getRuntime().exec("grep -c 'TAG' /home/cc/test");
int code = p.waitFor();
InputStream is = p.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
while(true){
String line = br.readLine();
if(line == null){
break;
}
if(line.trim().length()>0){
sb.append(line);
sb.append("\n");
}
}
br.close();
isr.close();
is.close();
System.out.println(code);//
System.out.println(sb.toString());
System.out.println("=====================");
InputStream eis = p.getErrorStream();
InputStreamReader eisr = new InputStreamReader(eis);
BufferedReader ebr = new BufferedReader(eisr);
StringBuilder esb = new StringBuilder();
while(true){
String eline = ebr.readLine();
if(eline==null){
break;
}
if(eline.trim().length()>0){
esb.append(eline);
esb.append("\n");
}
}
ebr.close();
eisr.close();
eis.close();
System.out.println(esb.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}用ProcessBuilder 构造 的命令 就可以直接调用grep 好像不能正常终止
你加上一个回车换行 就能得到正确结果,如果不加 会认为你的文件没有结束,导致命令不能执行完成!
以上问题原因在于exec这个方法的参数是个String数组,我在代码中将命令写在一个String串里面,这样的话,exec在收到参数时,会自动调用split方法以空格作为分隔符将string串切分成一个String类型的数组,这样命令中TAG实际上在数组中存成了“‘TAG’”,多了个单引号,3楼sotom提出的解决办法能解决这个问题,但是如果对于有的命令,这个单引号是不能去掉的,比如 grep -E 'TAA|TAA|TGA' test.txt这个命令,如果去掉单引号,命令将不能执行。
所以,我的建议是,以后大家如果要在java中调用shell,可以这么写命令,如
String[] cmd= { "grep", "-c", "TAG", outpath };
将命令存储在字符串数组中,这样一般就不会有上述问题了~