Reputation: 1804
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.
Upvotes: 2
Views: 68
Reputation: 41271
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:
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.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.
Upvotes: 2