2013-04-11 38 views
0

我想在java中使用下面给出的commnda。我使用了RunTime。但有了RunTime,我的所有时间都是空的。如何使用Runtime?

$ cut -d. -f2,3 <<< com.tata.titi.toto 
tata.titi 

在Java中使用的方法,包括:

public void tataName() { 

    try { 
     Process process = Runtime.getRuntime().exec(
       new String[] { "/bin/sh", "cut -d. -f2,3 <<< com.tata.titi.toto " }); 
     process.waitFor(); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(
       process.getInputStream())); 
     File f = new File(path+ "/taname.txt"); 
     PrintWriter writer = new PrintWriter(f, "UTF-8"); 
     String line = reader.readLine(); 
     while ((line != null)) { 
      System.out.println(line); 
      writer.println(line); 
      line = reader.readLine(); 



     } 

     writer.close(); 


    } catch (IOException | InterruptedException e1) { 

    } 
+0

HTTP:// WWW。 rgagnon.com/javadetails/java-0014.html但我会使用版本与输入和错误STRAM只是等待单独的线程上的错误,所以一个进程可以发出很少的输出,然后错误,然后更多的输出.... – tgkprog 2013-04-11 13:50:40

回答

1

process.waitFor();删除。您正在运行进程,等待其终止,然后尝试在其太晚时读取其输出。

如果您删除此行,则执行该过程并读取其输出。我希望这能帮到您。

顺便说一句,你为什么这样做?您可以逐行读取文件,并在java中分割每一行。这是很多更容易和跨平台。

+0

有些时候,我将有路径/ com.tata.titi.toto。我不知道是否更容易使用拆分。事实上,我有一个与许多应用程序的文件夹,命名为他们的包如com.tata.titi.toto.apk,应用程序的名称是tata.titi。我想有一个地图<包,应用程序名称> – diez 2013-04-11 13:38:48

+0

它不是等待我用process.wait的一些综合的问题,因为它是好的,当我更改命令时,我有我想要的。 – diez 2013-04-11 13:41:48

0

http://www.rgagnon.com/javadetails/java-0014.html但在一个单独的线程做错误流处理为什么 - 见的javadoc http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Process.html

因为有些本机平台仅针对 标准输入和输出流提供有限的缓冲区大小,未能及时写输入 流或读取子流程的输出流可能会导致子进程阻塞,甚至死锁。

package utl; 

import java.io.BufferedReader; 
import java.io.FileInputStream; 
import java.io.FileReader;  

public class ReadStreamAsync extends Thread { 

    private BufferedReader is = null; 
    private int toOut = 0;//0 none, 1 out, 2 err 
    private boolean toSB = false; 
    private StringBuffer sb = null; 
    public ReadStreamAsync(BufferedReader is, int toOut, boolean toSB){ 
     if(is == null)throw new NullPointerException("stream is null"); 
     this.is = is; 
     this.toSB = toSB; 
     this.toOut = toOut; 
     if(toSB)sb = new StringBuffer(); 
     start(); 
    } 

    public void run(){ 
     try{ 
      int i; 
      while((i = is.read()) > -1){ 
       if(toOut == 1){ 
        System.out.print((char)i); 
       }else if(toOut ==2){ 
        System.err.print((char)i); 
       } 
       if(toSB)sb.append((char)i); 
      } 
     }catch(Exception e){ 
      e.printStackTrace(); 
     }finally{ 
        try{ 
          is.close(); 
        }catch(Exception e){ 
          e.printStackTrace(); 
        } 
       } 
    } 

    public String getRead(){ 
     return sb.toString(); 
    } 

    /** 
     * test code 
    * @param args 
    */ 
    public static void main(String[] args) { 
     try{ 
      BufferedReader fis = new BufferedReader(new FileReader("c:/tmp/sample1.txt")); 
      ReadStreamAsync t = new ReadStreamAsync(fis, 1, false); 
      t.join(1000); 
      System.out.println("\nAll :"); 
      fis = new BufferedReader(new FileReader("c:/tmp/sample1.txt")); 
      t = new ReadStreamAsync(fis, 0, true); 
      t.join(1000); 
      System.out.println(t.getRead()); 

     }catch(Exception e){ 
      e.printStackTrace(); 
     } 

    } 

} 

这就是你如何使用它

 Process proc = procBldr.start(); 
     //props is a java.util.properties that was previosly loaded from some stream 
     System.out.println(" Cmd start f :" + cmdStartFolder); 
     System.out.println(" Cmd :" + lstCmds); 
     BufferedReader isO = new BufferedReader (new InputStreamReader(proc.getInputStream())); 
     BufferedReader isE = new BufferedReader (new InputStreamReader(proc.getErrorStream())); 
     asynO = new com.enstage.commonutil.file.ReadStreamAsync(isO, 1, true); 
     asynE = new com.enstage.commonutil.file.ReadStreamAsync(isE, 1, true); 
     if("1".equals(props.getProperty("waitFor")){ 
      proc.waitFor();//maybe parameterize this not required everywhere only good for short processes 
     } 
     String sleepAfterWait = props.getProperty("sleepAfterWait"); 
     try { 
      Thread.sleep(500);//some sleep after telling windows to do things with files is good 
      if(sleepAfterWait != null){ 
       int i = Integer.parseInt(sleepAfterWait); 
       Thread.sleep(i); 
      } 
     } catch (Exception e) { 
      System.out.println("sleep err :" + e); 
      e.printStackTrace(); 
     } 
     asynE.join(); 
     asynO.join(); 
     String checkString = props.getProperty("checkString"); 
     System.out.println("\n done "); 
     //asynE.getRead();//if you want error out as a string 
     if(checkString != null){ 
      System.out.println("checkString :" + checkString); 
      String out = asynO.getRead(); 
      if(out.indexOf(checkString) > -1){ 
       System.out.println(" Check string found!"); 
      }else{ 
       System.err.println(" *** Check string not found ***!"); 
      } 
     } 

已经用它成功地调用XCOPY在蝙蝠和其他进程短期和长期

相关问题