does java have a ' system ("PAUSE") ' like c++....if so what is the syntax to do it.

ty

Dani AI

Generated

A Java console app does not have a direct equivalent to C/C++ system("PAUSE"). What you usually want is to block on input at the very end so the user can read the output. As hinted, that is different from pausing a thread with Thread.sleep(...) (which just delays time, not waits for the user).

For a simple, portable "press Enter to exit" that works whether a real console is attached or you are in an IDE (where System.console() may be null), use:

static void waitForExit() {
    try {
        java.io.Console con = System.console();
        if (con != null) {
            con.readLine("Press Enter to exit...");
        } else {
            // Likely running in an IDE; fall back to System.in
            new java.io.BufferedReader(
                new java.io.InputStreamReader(System.in)
            ).readLine();
        }
    } catch (java.io.IOException ignored) {
        // Optionally log
    }
}

Call waitForExit(); right before main returns. Tip: do not close System.in or the BufferedReader/Scanner you create just for this pause, or later reads will fail.

If your app is GUI-based, a tiny modal dialog is cleaner than a console pause:

javax.swing.JOptionPane.showMessageDialog(null, "Done. Click OK to exit.");

As notes, if you launch from a shell, the output naturally stays visible. The window typically disappears only when double-clicking a program, so another pragmatic option is to run it from a terminal or configure your IDE to keep the console open. Finally, avoid spawning OS-specific commands to mimic pause; it ties you to one platform and adds unnecessary process overhead.

Recommended Answers

All 4 Replies

Hi Everyone,

Please explain in detail, pause a thread or pause while waiting for input from user??

Richard West

pause after reading user's input so that the user has a chance to read the screen before the program terminates

If you need something like that your architecture is wrong :)

Commandline programs are executed from a shell, so the output remains visible if the user wants it. If not he'll likely have it redirected to a file and why should you force him to look at something he doesn't want to see?

Hi everyone,

Here is a link with some code

http://www.rgagnon.com/javadetails/java-0145.html

do not forget to run your method in a separate thread other from the main thread

Here is another link to pause and resume the console printing of huge data

I hope this helps you

Yours Sincerely

Richard West

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.