14

Is it possible to read console input just before the embedded tomcat on the spring boot starts? The supposedly application flow is, request username and password from the user, and that will be used to be able to start the application. It works when using the java -jar command, the problem is when I close the console(SSH on linux) the process stops. I tried searching about it and found out that the process is tied to the console, so I tried using nohup, the problem is I cannot request for console input when using that. Is there any other way?

Eldon Hipolito
  • 704
  • 1
  • 8
  • 24

4 Answers4

12

I think this can help you.

public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);

    //  prompt for the user's name
    System.out.print("Enter your name: ");

    // get their input as a String
    String username = scanner.next();

    System.out.println(username);

    SpringApplication.run(Application.class, args);
}
utkusonmez
  • 1,486
  • 15
  • 22
  • Shouldn't you close the scanner, too? – Impulse The Fox Apr 13 '17 at 11:12
  • 4
    @ImpulseTheFox normally the rule of thumb is to always close the scanner. When it comes to `System.in`, it's slightly different. If you close `System.in`, you can't use it again within the life-span of your application. So if you need it more than once you leave it open. This _only_ applies to `System.in`, though. You still need to close everything else. – Niko Dec 19 '17 at 05:29
3

Get username and password in shell script before executing your java program and pass those as argument to your application.

#!/bin/bash
# Ask login details
read -p 'user: ' uservar
read -sp 'password: ' passvar
echo

Now you have user and password you can run java command with nohup and pass user password as jvm properties. You can also pass user and password as program arguments as suggested in other answer.

Like nohup java -jar abc.jar -Duser=$user -Dpassword=$password

And fetch these properties using

 String user = System.getProperty("String");
 String password = System.getProperty("password");
Vipin
  • 4,851
  • 3
  • 35
  • 65
2

You can use nohup to start the jar with the parameters, they just won't be prompted for them on new lines in the terminal. The User could add them as parameters when starting the jar. See details below.

Example:

Main Class

public static void main(String[] args) {
        String username = args[0];
        String password = args[1];
        if(username.equals("admin") && password.equals("password")) {
            SpringApplication.run(NohupApplication.class, args);
        } else {
            System.out.println("You are not authorized to start this application.");
        }
    }

With Invalid Credentials

Command

nohup java -jar example.jar user password

nohup.out

You are not authorized to start this application.

With Valid Credentials

Command

nohup java -jar example.jar admin password

nohup.out

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.2.RELEASE)
Kyle Anderson
  • 6,801
  • 1
  • 29
  • 41
1

If you want to full log with console input like logger + System.out.println() outputs, then you have to use

nohup java -jar yourJarFile.jar admin password >> fullLogOutput.log &

Another issue:

the problem is when I close the console(SSH on linux) the process stops.

So you want to run your jar file as background process. You can't stop it with your console closing. Just add & symbol after full command, it will not stop. You can use the following command.

nohup java -jar yourJarFile.jar &

For taking full log with console output

nohup java -jar yourJarFile.jar >> fullLogOutput.log &

The & symbol is used to run the program in the background.

The nohup utility makes the command passed as an argument run in the background even after you log out.

Stopping/Killing the back-end process:

For stopping the background process, use ps -aux to get the id and then kill (id number)

ps -aux | grep yourJarFile.jar

You will get the id number. To kill that

sudo kill -9 <pid>

Resource Link: https://stackoverflow.com/a/12108646/2293534

Community
  • 1
  • 1
SkyWalker
  • 28,384
  • 14
  • 74
  • 132