Comandos do cmd pelo Java

4 respostas
java
PedroAlves

Olá, criei um jbutton para executar um comando CMD.
O resultado não esta aparecendo totalmente. O que fazer para corrigir? Obrigado.

Código:

try { Process process = Runtime.getRuntime().exec("ipconfig"); Scanner leitor = new Scanner(process.getInputStream()); while (leitor.hasNextLine()) { //System.out.println(leitor.nextLine()); jTextPane1.setText(leitor.nextLine()); } } catch (IOException e) { e.printStackTrace(); }

4 Respostas

peczenyj

Salvo engano o setText vai “setar o texto”.

nesse caso vai aparecer a ultima linha.

o que vc deveria fazer:

  1. cria um StringBuffer.
  2. adiciona cada linha no StringBuffer
  3. converte pra String e usa no setText
PedroAlves

Olá OBG, cria um StringBuffer. Mas, continua o mesmo erro.

`        try {

Process process = Runtime.getRuntime().exec(ipconfig);

Scanner leitor = new Scanner(process.getInputStream());

while (leitor.hasNextLine()) {

StringBuffer sb = new StringBuffer();

sb.append(leitor.nextLine());
jTextPane2.setText(sb.toString());
    }
} catch (IOException e) {

e.printStackTrace();

}`
peczenyj

quem sabe tu initaliza o string buffer fora do while

PedroAlves

Obrigado peczenyj. Funcionou.

Criado 20 de setembro de 2016
Ultima resposta 20 de set. de 2016
Respostas 4
Participantes 2