이 문제가 발생합니다.
내 프로그램은 Windows 플랫폼에서 Runtime.getRuntime().exec(cmd);
을 호출합니다. 오류 및 출력 스트림을 읽고이 출력을 사용하여 작업을 수행합니다. 이 메소드는 각 4-5 초 후에 루프에서 호출되며 프로그램이 종료 될 때까지 계속됩니다.Runtime.getRuntime(). exec (cmd); 새로운 실행마다 이전 출력을 추가합니까?
이제 출력을 읽을 때마다 이전 출력이 새 출력에 추가되고 각 반복마다 결과가 커집니다. 어쨌든이 일을 막을 수 있습니까? 실행 된 명령은 일부 필터링 매개 변수가있는 "작업 목록"입니다.
이 Runtime.getTuntime(). exec (cmd)에 대한 메소드 (문자열 출력을 반환 함)를 작성했는데 실행 후 프로세스를 닫고 있지만 루프 내에서 이전에 호출 될 때마다 출력이 새로운 것에 추가됩니다. 여기
코드입니다 : 클래스 fList에서class Track implements Runnable {
static int size = 0;
public void run() {
String cmd1 = "tasklist /fo list /fi \"imagename eq java.exe\"";
String cmd2 = "tasklist /fo list /fi \"imagename eq javaw.exe\"";
String text = "";
int i=1, j=0;
while(size < 100000){
try{
text = fList.pList(cmd2, 1);
if (text.indexOf("javaw.exe")== -1){
text = fList.pList(cmd1, 1);
}
if(j==22) System.out.println(text);
if (text.charAt(0)!= '0') continue;
i = text.lastIndexOf("Mem Usage: ")+14;
text = text.substring(i);
text = text.substring(0,text.lastIndexOf(" K"));
text = text.replaceFirst(",", "");
size = Integer.parseInt(text);
System.out.println(size);
Thread.sleep(3000);
j++;
} catch(Exception e){
System.out.println(e);
}
}
System.out.println("Memory utlization exceeded the permissible limit");
System.out.println("Now terminating the Program\n");
System.exit(1);
}
static void memoryCheck(int size) throws Exception{
(new Thread(new Track())).start();
}
}
는 방법 PLIST입니다 : 모두 여기에 복사 iv'e
static String pList(String cmd, int eval) throws Exception{ //can execute external command
String out = "";
int val = 5; // should not be zero, to verify returned code zero for normal exec.
try
{
//String osName = System.getProperty("os.name");
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd);
// any error message?
eProcList error = new eProcList(proc.getErrorStream());
// any output?
eProcList output = new eProcList(proc.getInputStream());
// kick them off
error.start();
output.start();
// any error???
int exitVal = proc.waitFor();
out = eProcList.procList();
val = exitVal;
proc.destroy();
proc.getInputStream().close();
proc.getErrorStream().close();
} catch (Throwable t)
{
t.printStackTrace();
}
if (eval==1) return val + out;
return out;
}
class eProcList extends Thread
{
InputStream iStream;
static String oPut = "";
eProcList(InputStream iStream)
{
this.iStream = iStream;
}
public void run()
{
try
{
InputStreamReader isr = new InputStreamReader(iStream);
BufferedReader br = new BufferedReader(isr);
String line=null;
while ((line = br.readLine()) != null)
oPut = oPut + line+"\n";
} catch (IOException e)
{
e.printStackTrace();
}
}
static public String procList(){
return oPut;
}
}
그렇게 물었다.
출력을 읽는 코드를 게시해야합니다. –
Java 클래스 이름은 대문자 'LikeThis'로 시작해야합니다. 코드를 훨씬 쉽게 읽을 수 있도록하는 규칙입니다. –