Java 如何使用运行时?
我想用java使用下面给出的commnda。我使用了RunTime。但在运行时,结果是所有时间都为空Java 如何使用运行时?,java,shell,runtime,Java,Shell,Runtime,我想用java使用下面给出的commnda。我使用了RunTime。但在运行时,结果是所有时间都为空 $ cut -d. -f2,3 <<< com.tata.titi.toto tata.titi $cut-d-f2,3删除进程waitFor()。您正在运行进程,等待其终止,然后在太晚时尝试读取其输出 如果删除该行,则执行该进程并读取其输出。我希望这对你有帮助 顺便说一句,你为什么要这样做?您可以逐行读取文件,并在java中拆分每一行。这更简单,跨平台。但是在单独的线程中处
$ cut -d. -f2,3 <<< com.tata.titi.toto
tata.titi
$cut-d-f2,3删除进程waitFor()代码>。您正在运行进程,等待其终止,然后在太晚时尝试读取其输出
如果删除该行,则执行该进程并读取其输出。我希望这对你有帮助
顺便说一句,你为什么要这样做?您可以逐行读取文件,并在java中拆分每一行。这更简单,跨平台。但是在单独的线程中处理错误流为什么?请参阅javadoc
因为某些本机平台仅为提供有限的缓冲区大小
标准输入和输出流,未能及时写入输入
流或读取子流程的输出流可能会导致
子进程阻塞,甚至死锁
这就是你可以使用它的方式
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 ***!" );
}
}
如果成功地使用它调用.bat中的xcopy和其他短流程和长流程过一段时间,我将拥有path/com.tata.titi.toto。我不知道使用拆分是否更容易。事实上,我有一个包含许多应用程序的文件夹,命名为com.tata.titi.toto.apk,应用程序的名称是tata.titi。我希望有一个映射,这不是等待的问题,我使用了一些synthese和process。等待一个,它是好的,当我更改命令时,我得到了我想要的。但我会使用带有输入和错误字符串的版本,只需在单独的线程上等待错误,这样一个进程就可以发送很少的输出,然后是错误,然后是更多的输出。。。。
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 ***!" );
}
}