2010-12-15 2 views
5

laufen muß ich von Java eine Batch-Skript auszuführen, dieJava: Erkennen Benutzereingabeaufforderung, wenn eine Batch-Skript von Java

folgenden nicht

1) Sobald es gestartet wird es eine langwierige Aufgabe (bis zu mehreren Sekunden) führt.

2) Danach erscheint die Aufforderung "Passwort:".

3) Dann gibt der Benutzer das Passwort ein und drückt die Eingabetaste.

4) Dann schließt das Skript seine Arbeit ab.

Ich weiß, wie man das Skript von Java aus startet, ich weiß, wie man die Ausgabe des Batch - Skripts in Java liest, aber ich weiß nicht, wie ich auf die Passwortabfrage warten soll Batch-Skript wartet auf die Passworteingabe).

Meine Frage ist also: Wie erfahren Sie, wenn das Batch-Skript die Eingabeaufforderung gedruckt hat?

Im Moment habe ich folgenden Code:

final Runtime runtime = Runtime.getRuntime(); 
final String command = ... ; 

final Process proc = runtime.exec(command, null, this.parentDirectory); 

final BufferedReader input = new BufferedReader(new InputStreamReader(
    proc.getInputStream())); 

String line = null; 

while ((line = input.readLine()) != null) { 
LOGGER.debug("proc: " + line); 
} 

Antwort

3

, die den Job tun sollten:

public static void main(final String... args) throws IOException, InterruptedException { 
    final Runtime runtime = Runtime.getRuntime(); 
    final String command = "..."; // cmd.exe 

    final Process proc = runtime.exec(command, null, new File(".")); 

    final BufferedReader input = new BufferedReader(new InputStreamReader(proc.getInputStream())); 

    StringBuilder sb = new StringBuilder(); 
    char[] cbuf = new char[100]; 
    while (input.read(cbuf) != -1) { 
     sb.append(cbuf); 
     if (sb.toString().contains("Password:")) { 
      break; 
     } 
     Thread.sleep(1000); 
    } 
    System.out.println(sb); 
} 
+0

Bist Du sicher ist getOutputStream()? Im Moment kann ich einen Teil der Ausgabe des Batch-Skripts mit input.readLine() sehen (siehe Logging-Anweisung in meinem Code-Snippet). Aber nicht alles - ich kann die Aufforderung auf diese Weise nicht erkennen. –

+0

meine erste Antwort war Unsinn. Der Javadoc erklärt den Ausgabe- und Eingabestream: http://download.oracle.com/javase/6/docs/api/java/lang/Process.html. – remipod

+0

Danke für deinen Code. Leider wird die Passwortabfrage NICHT durch einen Zeilenumbruch beendet. Deshalb funktioniert Readline in diesem Fall nicht. –

1

Dieses scheint zu funktionieren:

@Override 
public void run() throws IOException, InterruptedException { 
    final Runtime runtime = Runtime.getRuntime(); 
    final String command = ...; 

    final Process proc = runtime.exec(command, null, this.parentDirectory); 

    final BufferedReader input = new BufferedReader(new InputStreamReader(
      proc.getInputStream())); 

    String batchFileOutput = ""; 

    while (input.ready()) { 
     char character = (char) input.read(); 
     batchFileOutput = batchFileOutput + character; 
    } 

    // Batch script has printed the banner 
    // Wait for the password prompt 
    while (!input.ready()) { 
     Thread.sleep(1000); 
    } 

    // The password prompt isn't terminated by a newline - that's why we can't use readLine. 
    // Instead, we need to read the stuff character by character. 
    batchFileOutput = ""; 

    while (input.ready() && (!batchFileOutput.endsWith("Password: "))) { 
     char character = (char) input.read(); 
     batchFileOutput = batchFileOutput + character; 
    } 

    // When we are here, the prompt has been printed 
    // It's time to enter the password 

    if (batchFileOutput.endsWith("Password: ")) { 
     final BufferedWriter writer = new BufferedWriter(
       new OutputStreamWriter(proc.getOutputStream())); 

     writer.write(this.password); 

     // Simulate pressing of the Enter key 
     writer.newLine(); 

     // Flush the stream, otherwise it doesn't work 
     writer.flush(); 
    } 

    // Now print out the output of the batch script AFTER we have provided it with a password 
    String line; 

    while ((line = input.readLine()) != null) { 
     LOGGER.debug("proc: " + line); 
    } 

    // Print out the stuff on stderr, if the batch script has written something into it 
    final BufferedReader error = new BufferedReader(new InputStreamReader(
      proc.getErrorStream())); 

    String errorLine = null; 

    while ((errorLine = error.readLine()) != null) { 
     LOGGER.debug("proc2: " + errorLine); 
    } 

    // Wait until the program has completed 

    final int result = proc.waitFor(); 

    // Log the result 
    LOGGER.debug("result: " + result); 
} 
Verwandte Themen