Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Press Any Key To Continue" is holding up my program

Tags:

java

Essentially, I'm running a silent command-line scan using Java. I then wait for this scan to finish and the process to close before moving on. However, the process doesn't end until you "Press any key to continue". Another thing to note is that the command line window is not visible at the time, which is intended, so the process just stays active in the background, idle, once the scan is done. Here is a snippet of the code:

Main.print("Performing RKR Scan...");
try {
    Process p1 = Runtime.getRuntime().exec(dir + "RootkitRemover.exe /noupdate"); 
    try {
        p1.waitFor();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
} catch (IOException e) {
    Main.print("Error Scanning With RKR: " + e);
}

Is there any way of getting through this "Press any key" thing, whether it be an official solution or just a bypass?

like image 830
JTApps Avatar asked Aug 01 '13 13:08

JTApps


1 Answers

As I said in my comment, you could probably listen to the InputStream until the "Press any key". Then you should write a newLine to the process via the OutputStream. Here is some code to help you:

   public class BatchEnter {

    public static void main(String[] args) throws Exception {
        List<String> params = Arrays.asList(new String[] { "cmd", "/C", "C:/test/test.bat" });
        ProcessBuilder builder = new ProcessBuilder(params);
        builder.directory(new File("C:/test")).redirectErrorStream(true);
        final Process p = builder.start();

        final BufferedReader wr = new BufferedReader(new InputStreamReader(p.getInputStream()));
        final BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(p.getOutputStream()));
        String line = "";
        try {
            while ((line = wr.readLine()) != null) {
                if (line.equals("Press any key")) {
                    String newLine = "\n\r";
                    writer.write(newLine);

                }
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        p.waitFor();
    }
}

Some remark on the code:

  • You should change the exception managing cause I'm just doing e.printStackTrace()
  • My process is a command window, I don't know if it will work for your exe

Edit:

If you're not getting any output, you could simply try to send the newLine feeds until the program finish. You could for exemample, write the newLine each second until the process stop. Something like :

final BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(p.getOutputStream()));
    while(true){
        String newLine = "\n\r";
        writer.write(newLine);
    }

You could start a thread that write to the process, and stop it when the p.waitFor() return. You should change the condition too, for something more clean like each second or while the thread is not stop. Beware that you will have IOException if you're writting to the stream and the process finish. And this is a really not something I would rely on, but if it could help you.

like image 127
Marc-Andre Avatar answered Oct 29 '22 04:10

Marc-Andre