Java应用程序无法从控制台获取输入

Java应用程序无法从控制台获取输入

本文介绍了Docker Java应用程序无法从控制台获取输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的Java应用程序创建一个docker映像.在启动时,需要为此应用程序提供一个密码(当前是通过控制台).

I am trying to create a docker image for my java application. At startup this application needs to be given a password (currently via console).

我尝试了几种获取输入的方法,但是都失败了.这是docker的限制吗?如果可以,有什么解决方法?

I tried several methods of obtaining input however they have all failed. Is this a limitation of docker and if so is there a workaround?

此代码段:

Console console = System.console();
if(console == null){
    System.out.println("console is null!!");
} else {
    System.out.println("Input password: ");
    char[] password = console.readPassword("Pass: ");
}

System.console()返回null.

此代码段:

    System.out.println("Creating InputStreamReader");
    InputStreamReader s = new InputStreamReader(System.in);
    System.out.println("Creating BufferedReader");
    BufferedReader r = new BufferedReader(s);
    System.out.println("Input password: ");
    String password = r.readLine();
    System.out.println("Password: "+password);

输入将被自动跳过(导致String密码为null),并且程序将继续执行,就像没有请求输入一样. (密码为null)

the input is automatically skipped, (resulting in the String password to be null) with the program continuing execution as if there was no input requested. (password is null)

此代码段:

Scanner s = new Scanner(System.in);
System.out.println("Input password: ");
String password = s.next();

我知道

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:907)
    at java.util.Scanner.next(Scanner.java:1416)
    at com.docker.test.DockerTest.testScanner(DockerTest.java:49)
    etc...

我正在使用docker run test/plaintest1

我的dockerfile如下

my dockerfile is as follows

FROM centos
RUN yum install -y java-1.7.0-openjdk
ADD DockerTest.jar /opt/ssm
ENTRYPOINT ["java","-jar","/opt/ssm/DockerTest.jar"]
CMD [""]

推荐答案

已解决.

通过使用-i和-t参数运行命令,可以允许您输入密码.使用所有3种方法.

By running the command using the -i and -t parameters you can be allowed to enter the password. using all 3 methods.

所以基本上是docker run -i -t <imagename> <params>

这篇关于Docker Java应用程序无法从控制台获取输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 01:27