2010-12-15 19 views
5

muszę wykonać z Java skrypt wsadowy, który ma następująceJava: Wykrywanie wiersz użytkownika po uruchomieniu skryptu wsadowego z Java

1) Po jej uruchomieniu wykonuje długi (do kilku sekund) zadanie.

2) Następnie wyświetla monit "Hasło:".

3) Następnie użytkownik wpisze hasło i naciśnie klawisz Enter.

4) Następnie skrypt wykonuje swoje zadanie.

Wiem, jak uruchomić skrypt z Java, wiem, jak odczytać wyjście skryptu wsadowego w Javie, ale nie wiem, jak czekać na pojawienie się monitu o hasło (jak się dowiedzieć, że skrypt wsadowy oczekuje na wprowadzenie hasła).

Moje pytanie brzmi: Jak się dowiedzieć, kiedy skrypt wsadowy wydrukował monit?

Obecnie mam następujący kod:

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); 
} 

Odpowiedz

3

To powinno załatwić sprawę:

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

Czy jesteś pewien, że jest getOutputStream()? W tej chwili widzę część danych wyjściowych skryptu wsadowego za pomocą metody input.readLine() (zobacz instrukcję rejestrowania w moim fragmencie kodu). Ale nie wszystkie - nie mogę wykryć zachęty w ten sposób. –

+0

moja pierwsza odpowiedź była nonsensem. Jawadoc wyjaśnia strumień wyjściowy i wejściowy: http://download.oracle.com/javase/6/docs/api/java/lang/Process.html. – remipod

+0

Dzięki za twój kod. Niestety, zapytanie o hasło NIE jest zakończone znakiem nowej linii. Dlatego readline nie działa w tym przypadku. –

1

Ten wydaje się działać:

@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); 
} 
Powiązane problemy