Tuesday, November 30, 2021

Java Application With User Input in Docker Container

In this tutorial you will see how to run a Java program that needs user input using Docker so the aim is to dockerize a Java application so that the Docker container is interactive.

1. Java program

We'll start by writing a Java program that uses Scanner to take input from console and checks whether the entered number is a prime number or not.

package org.netjs.prgrm;

import java.util.Scanner;

public class PrimeNumber {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Please enter a number: ");
    int num = sc.nextInt();
    boolean isPrime = true;  
    for(int i = 2; i < num/2; i++) {
      if(num % i == 0) {
        isPrime = false;
        break;
      }
    }  
    if(isPrime)
      System.out.println(num + " is a prime number");
    else
      System.out.println(num + " is not a prime number");

    sc.close();
  }
}

2. Creating Dockerfile

FROM openjdk:12-alpine

COPY . /usr/src/myjavaapp

WORKDIR /usr/src/myjavaapp

RUN javac PrimeNumber.java -d bin

CMD ["java", "-cp", "bin", "org.netjs.prgrm.PrimeNumber"]

For detailed explanation of Dockerfile instructions please refer this post- Run Java Application in Docker Container

Note here that -d option is used with javac to set the destination directory for the class files. When the PrimeNumber.java is compiled the resultant .class file is stored in the bin directory (with in the WORKDIR /usr/src/myjavaapp) specified with the -d option.

Another thing to note here is that package is also specified in the Java file that is why fully qualified name is given in the CMD instruction when the program is executed.

3. Build image

Following command builds the image and names the image as java-app.

docker build -t java-app .

4. Run Docker Image

Once the Docker image is created successfully you can run it using the docker run command by providing the name of the image which you want to run. But remember that Docker container has to be interactive, running it simply using run command results in an error as you won't be able to provide input.

D:\NETJS\java-docker>docker run java-app

Please enter a number:
Exception in thread "main" java.util.NoSuchElementException
        at java.base/java.util.Scanner.throwFor(Scanner.java:937)
        at java.base/java.util.Scanner.next(Scanner.java:1594)
        at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
        at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
        at org.netjs.prgrm.PrimeNumber.main(PrimeNumber.java:10)

5. Making Docker container interactive

Docker run command can take the following two options to make it interactive.

-i or -interactive: Keep STDIN open even if not attached

-t, --tty: Allocate a pseudo-TTY

Using these options with the run command does the trick.

 
D:\NETJS\java-docker>docker run -it java-app

Please enter a number:
7
7 is a prime number

That's all for this topic Java Application With User Input in Docker Container. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. Run Python Application in Docker Container

You may also like-

  1. AtomicInteger in Java With Examples
  2. Race Condition in Java Multi-Threading
  3. New Date And Time API in Java With Examples
  4. Pre-defined Functional Interfaces in Java
  5. Difference Between Encapsulation And Abstraction in Java
  6. How to Read File From The Last Line in Java
  7. Spring util-namespace Example For Wiring Collection
  8. Angular Route - Passing Static Data

No comments:

Post a Comment