Ciao a tutti, vi spiego subito il mio problema. Ho utilizzato due classi per poter eseguire un programma esterno. Le classi sono :
import java.util.*;
import java.io.*;
public class StreamGobbler extends Thread
{
InputStream is;
String type;
StreamGobbler(InputStream is, String type)
{
this.is = is;
this.type = type;
}
public void run()
{
try
{
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null)
System.out.println(type + ">" + line);
} catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
e l'altra classe con il comando da eseguire è :
public class GoodWindowsExec
{
public static void main(String args[])
{
//if (args.length < 1)
//{
// System.out.println("USAGE: java GoodWindowsExec <cmd>");
//System.exit(1);
//}
try
{
String osName = System.getProperty("os.name" );
String[] cmd = new String[3];
cmd[0] = "cmd.exe" ;
cmd[1] = "/C" ;
cmd[2] = "wmic /node: /user: /password:computersystem get caption";
Runtime rt = Runtime.getRuntime();
System.out.println("Execing " + cmd[0] + " " + cmd[1]
+ " " + cmd[2]);
Process proc = rt.exec(cmd);
// any error message?
StreamGobbler errorGobbler = new
StreamGobbler(proc.getErrorStream(), "ERROR");
// any output?
StreamGobbler outputGobbler = new
StreamGobbler(proc.getInputStream(), "OUTPUT");
// kick them off
errorGobbler.start();
outputGobbler.start();
// any error???
int exitVal = proc.waitFor();
System.out.println("ExitValue: " + exitVal);
} catch (Throwable t)
{
t.printStackTrace();
}
}
}
chiaramente nel comando dovè c'è /node: /user: /password ci sono i dati corretti, ovvero io, username del pc al quale mi collego e la sua password.
Quello che accade è che quando faccio partire la classe GoodWinExec, il comando wmic funziona ma rimane attivo e non si conclude mai! ( cosa che invece accade quando lo lancio direttamente da console di windows ). Qualcuno sa spiegarmi perchè?
NB: lo so che utilizzare programmi esterni non è segno di buona programmazione e che è una pratica da evitare il più possibile ma in questo caso ho davvero bisogno di richiamare il wmic.