Salve povo,
Estou tentando pegar o mensagem de retorno do comando net use u:\\maquinaRede\c à partir do java, porém não faço idéia de como catar o outputstream da vida.
Eis o código, se alguém tiver um luz :-)
package mapeamentoderede;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
*
* @author Ricardo Spinoza
*/
public class Cmd {
private Parametros parametros;
private Mensagem msgRetorno;
public Cmd() {
parametros = new Parametros();
msgRetorno = new Mensagem();
}
public Mensagem montaMapeamento() {
msgRetorno.setStatus(true);
msgRetorno.setMensagem("Operação realizada com êxito");
String cmd = "cmd /c NET USE x: \127.0.0.1\c";
//String cmd = "cmd /c dir";
try {
Process p = Runtime.getRuntime().exec(cmd);
new CopyThread(p.getInputStream(), System.out).start();
new CopyThread(p.getErrorStream(), System.err).start();
//p.getOutputStream();
p.waitFor();
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
System.out.println();
// read the output from the command
String s = null;
System.out.println("Saida padrão output do command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Saída padrão de error do commando:\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
} catch (IOException ex) {
msgRetorno.setStatus(false);
msgRetorno.setMensagem("Falha tentar mapear a unidade de rede. \ndetalhes técnicos: " + ex.getLocalizedMessage());
} catch (InterruptedException ex) {
msgRetorno.setStatus(false);
msgRetorno.setMensagem("Falha tentar mapear a unidade de rede. \ndetalhes técnicos: " + ex.getLocalizedMessage());
}
return msgRetorno;
}
}
package mapeamentoderede;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* @author Ronald Tetsuo Miura
*/
public final class CopyThread extends Thread {
private static final Logger LOG = Logger.getLogger(CopyThread.class.getName());
private final InputStream in_;
private final OutputStream out_;
private final boolean autoFlush_;
public CopyThread(InputStream in, OutputStream out) {
this(in, out, false, true);
}
/**
33. * @param in -
34. * @param out -
35. * @param asDaemon -
36. * @param autoFlush -
37. */
public CopyThread(InputStream in, OutputStream out, boolean asDaemon, boolean autoFlush) {
setDaemon(asDaemon);
this.in_ = in;
this.out_ = out;
this.autoFlush_ = autoFlush;
}
/**
46. * @see java.lang.Thread#run()
47. */
public final void run() {
try {
int b;
do {
b = this.in_.read();
if (b != -1) {
writeOutput(b);
}
} while (b != -1);
} catch (IOException e) {
LOG.log(Level.SEVERE, null, e);
} finally {
closeOutput();
}
}
/**
65. * @param b -
66. * @throws IOException -
67. */
private void writeOutput(int b) throws IOException {
if (this.out_ != null) {
this.out_.write(b);
if (this.autoFlush_) {
this.out_.flush();
}
}
}
/**
78. */
private void closeOutput() {
try {
if (this.out_ != null) {
this.out_.flush();
this.out_.close();
}
} catch (IOException e) {
LOG.log(Level.SEVERE, null, e);
}
}
}
A console qdo rodo o problema traz a seguinte mensagem:
Ocorreu o erro de sistema 53.
O caminho da rede n�o foi encontrado.
Gostaria de pegar esta mensagem pra fazer o retorno de erro. pensei em retornar um arquivo de log.txt da vida.
Alguma dica?