Home > database >  NosuchElementException while giving input to a docker run -v command
NosuchElementException while giving input to a docker run -v command

Time:05-16

Trying to run my docker image using the below command:

docker run -v c:/users/dmsss/downloads/green2/test_data:/src/java/data/ solution/me_javasolu java green.java data/test1.csv data/result.csv

I'm getting the following error:

Exception in thread "main" Give paths to input and output files:
null
java.lang.NullPointerException
        at green.main(green.java:39)

My green.main code snippet:

 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 System.out.println("Give paths to input and output files:");
 String names=br.readLine();
System.out.println(names);

As you can see in the place of "names" i'm getting null, instead of "data/test1.csv data/result.csv". I know that using: docker run -it, will give the desired outcome, but I want the inputs to be given in the same line as run

docker run -it solution/me_javasolu
Give paths to input and output files:
test_data/test3.csv test_data/result.csv
test_data/test3.csv test_data/result.csv
Profit: $99

Hope I get a solution to the problem, thanks in advance.

CodePudding user response:

You have already mapped volumes to your docker container. Try to use these paths inside your code instead of reading it from the console.

Paths.get("your path")

If you want to read from the console then you have to keep STDIN open which can be done by running your container in interactive mode.

CodePudding user response:

The arguments at the end of your docker command are passed as arguments to your java main method.

If you want to provide input on STDIN to your java application inside your container you need to do something like this:

docker run -v C:\Projects\Java\dockerJTest:/app openjdk /bin/bash -c 'echo stdin1 stdin2  | java -cp /app/ DockerJ arg1 arg2 arg3'
arg1
arg2
arg3
Give paths to input and output files:
stdin1 stdin2

public class DockerJ {
    public static void main (String[] args) {

        try {
            for (String arg : args) {
                System.out.println(arg);
            }
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Give paths to input and output files:");
            String names=br.readLine();
            System.out.println(names);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  • Related