2
public static void main(String[] args){
    System.out.println("enter password");
    char[] password = System.console.readPassword();
    if(passwordOK(password)){
        //let go of shell, and continue running.
    }
}

I want the Java application to keep on running, but stop "hogging" the cmd/terminal. Release cmd/terminal so that I can use it as per normal, while the application continues to run. No more terminal usage after password auth/setup.

12
  • 1
    I can't think of any terminal-based app that releases the terminal once it's done entering one piece of information. The convention I've seen is that it keeps the terminal until the entire application is done running. Commented Sep 12, 2017 at 14:54
  • Maybe this could help you: stackoverflow.com/questions/636367/… Commented Sep 12, 2017 at 14:58
  • 1
    This sounds like an XY problem. Please tell us what makes you try to design a program like that. What is your real task? What do you do with the terminal while the program is running? Commented Sep 12, 2017 at 14:58
  • @RealSkeptic for example, a SSL server that wants user input to decrypt a password-encrypted private key, then continues as a daemon. Commented Sep 12, 2017 at 15:00
  • 1
    @KlitosKyriacou I dunno about OP, but it really annoys me if I can't launch a server over a remote terminal. Commented Sep 12, 2017 at 15:33

1 Answer 1

2

I don't think a running process can switch itself from foreground to background, detaching from its input and output. A suitably sophisticated shell can, of course, use job management to background running tasks (in most UNIX shells, ctrl-Z, fg, bg etc.).

In Java the best I can think of is to write two programs:

  • your long-running server runs in the background, and starts by opening a ServerSocket, it listens for one connection, on which it expects to receive the authentication info. Only when that has happened does it initialise fully and go into its main listening loop.
  • another program runs in the foreground. It waits for the socket to become available, connects, prompts at the terminal for a password, writes that to the socket, closes and terminates

You'll need to do some simple scripting to launch both programs at the same time. There's no reason why both programs have to be Java -- on UNIX I'd be tempted to use netcat to squirt the password into the server.

For security, ensure that the socket uses localhost networking. On UNIX it could instead be a UNIX domain socket.

Of course there are lots of communication methods your two processes could use -- files, shared memory, pipes, TCP sockets, ... -- it's a matter of picking one that's secure enough for the information you're passing between them.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.