Lendo um processo do Windows (com "Tasklist")[RESOLVIDO]

3 respostas
M

Senhore, bom dia.

Estou tentando ler os processos do windows que estão em execução, preciso a principio ler o consumo de memoria de determinado processo.

Estou usando o RunTime para executar o Tasklist.
Consigo carregar o BufferedReader.
Mas não consigo ler a linha exata.

Será que tenho que salvar em um txt para e fazer o filtro na linha para capturar o consumo da memoria?

Segue o codigo:

public class processoWindows {   

    public static void main(String[] args) throws IOException {  
        Runtime runtime = Runtime.getRuntime();  
        String cmds[] = {"cmd", "/c","tasklist"};  
        Process proc = runtime.exec(cmds);  
        InputStream inputstream = proc.getInputStream();  
        InputStreamReader inputstreamreader = new InputStreamReader(inputstream);  
        BufferedReader bufferedreader = new BufferedReader(inputstreamreader);  
        String line = null;
        while ((line = bufferedreader.readLine()) != null) {  
            if (line.substring(0, 7).equalsIgnoreCase("cmd.exe")){//To tentando pegar primeiro a linha do processo, para depois tentar pegar o consumo da memoria               
                System.out.println(line);  
            }
        }
    }    
}

Segue o erro:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 6
	at java.lang.String.substring(String.java:1934)
	at processoWindows.main(processoWindows.java:37)
Java Result: 1

Obrigado.

3 Respostas

F

Tá dando excessão pq vc tá tentando acessar uma posição de string que não existe, ou não foi alocada…

Troca o if (line.substring(0, 7).equalsIgnoreCase(“cmd.exe”)) por if (line.contains(“cmd.exe”)).

Ve aí se funciona. :smiley:

M

Amigo,
Obrigado!
Era isso mesmo!

M

Segue o codigo p/ quem precisar:

import java.io.BufferedReader;  
import java.io.IOException;  
import java.io.InputStream;  
import java.io.InputStreamReader;  

public class processoWindows {   
    
    public static BufferedReader buffe() throws IOException{
        Runtime runtime = Runtime.getRuntime();  
        String cmds[] = {"cmd", "/c","tasklist"};  
        Process proc = runtime.exec(cmds);  
        InputStream inputstream = proc.getInputStream();  
        InputStreamReader inputstreamreader = new InputStreamReader(inputstream);  
        BufferedReader bufferedreader = new BufferedReader(inputstreamreader);  
        return bufferedreader;
    }
    
    public static int consumoMemoria(String processo) throws IOException{
        String linha = null;          
        int memoria = 0;
        BufferedReader bufferedreader = buffe();  
        
        while ((linha = bufferedreader.readLine()) != null) {  
            if (linha.contains(processo)){
                String n = linha.substring(60, 70);
                memoria = Integer.parseInt(String.valueOf(n.trim()).replaceAll("\\.", ""));                
                            
            }
        }
        return memoria;
    }
    public static int quantidadeProcessos(String processo) throws IOException{
        int contaProc = 0;
        String linha = null;          
        BufferedReader bufferedreader = buffe();  
        
        while ((linha = bufferedreader.readLine()) != null) {  
            if (linha.contains(processo)){
                contaProc++;               
            }
        }
        return contaProc;
    }

    public static void main(String[] args) throws IOException {  
        String ipServidor = java.net.InetAddress.getLocalHost().getHostAddress();
        System.out.println("IpServidor = "+ ipServidor);
               
        System.out.println("Memoria = " + consumoMemoria("Tomcat6.exe"));
        System.out.println("Quantidade Processos = " + quantidadeProcessos("chrome.exe"));
        
    }  
}
Criado 9 de maio de 2012
Ultima resposta 9 de mai. de 2012
Respostas 3
Participantes 2