Hallo Forum,
habe den Text im Internet gefunden und will es nachnutzen.
das Programm läuft, Aufruf mit 'java GoodWindowsExec java -version'
Allerdings zeigt es die ERROR - Meldung an. Warum ? der Exitwert ist auch 0.
Außerdem möchte ich die Ausgabe in eine Variable geschrieben haben. Die brauche ich an anderer Stelle in meinem Programm.
Gruß,
Onkel Tom
habe den Text im Internet gefunden und will es nachnutzen.
Java:
import java.util.*;
import java.io.*;
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();
}
}
}
public class GoodWindowsExec
{
public static void main(String[] args)
{
try
{
String osName = System.getProperty("os.name" );
String[] cmd = new String[4];
if( osName.equals( "Windows 10" ) )
{
cmd[0] = "cmd.exe" ;
cmd[1] = "/C" ;
cmd[2] = "java";
cmd[3] = "-version";
}
Runtime rt = Runtime.getRuntime();
System.out.println("Execing " + cmd[0] + " " + cmd[1] + " " + cmd[2]+" "+cmd[3]);
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();
}
}
}
das Programm läuft, Aufruf mit 'java GoodWindowsExec java -version'
Allerdings zeigt es die ERROR - Meldung an. Warum ? der Exitwert ist auch 0.
Außerdem möchte ich die Ausgabe in eine Variable geschrieben haben. Die brauche ich an anderer Stelle in meinem Programm.
Gruß,
Onkel Tom