On March 20, 2018, Java 10 was released and everybody wants to explore Java 10 but not everyone wants to change their development environment.

pasted image 0

So in this blog, we will be discussing how can we use Java 10 using Docker.

This can be done by following simple steps given below:

  1. Firstly, you need to install Docker. You can install Docker from the official site of Docker.
  2. Open terminal.
  3. Create a directory jdk10docker and move into that directory.
    mkdir jdk10docker
    cd jdk10docker
  4. Make a java class JdkTen.
    vi JdkTen.java
  5. Add the following code in your java class file.
    public class JdkTen {
        public static void main(String[] args) {
            System.out.println("Welcome to java 10! Enjoy exploring!");
            while (true) {
                try {
                } catch (Exception e) {
  6. Make a text file with the name Dockerfile.
    vi Dockerfile
  7. Copy following code in the Dockerfile.
    #using openjdk 10 as base image
    FROM openjdk:10
    #copying files from current directory from host to docker images
    COPY . .
    #Compiling JdkTen.java
    RUN javac JdkTen.java
    #executing JdkTen
    CMD ["java", "JdkTen"]
  8. Build docker image. It may take some time to download.
    docker build -t explorejava:10.1 ~/jdk10docker

    Which should result in the following output:

    Sending build context to Docker daemon 17.41kB
    Step 1/4 : FROM openjdk:10
    10: Pulling from library/openjdk
    2115d46e7396: Pull complete
    daa734ed5aa0: Pull complete
    801e6e5516c1: Pull complete
    8588f1af8d84: Pull complete
    916d2711f8ee: Pull complete
    7ce32ff39c36: Pull complete
    3be54ffb6c69: Pull complete
    fb555685447a: Pull complete
    Digest: sha256:3da180c49120b60bc8795660e7f90e2eb7df3a3d72f57aaa37008189138e311b
    Status: Downloaded newer image for openjdk:10
    ---> 4bb014411054
    Step 2/4 : COPY . .
    ---> 069e68b9a9d2
    Step 3/4 : RUN javac JdkTen.java
    ---> Running in 5d8a555f4dd2
    Removing intermediate container 5d8a555f4dd2
    ---> 862351887ea9
    Step 4/4 : CMD ["java", "JdkTen"]
    ---> Running in 9d7c61e46665
    Removing intermediate container 9d7c61e46665
    ---> 936181cf1c9e
    Successfully built 936181cf1c9e
    Successfully tagged explorejava:10.1
  9. Check for all the available docker images in your system using the following command:
    docker images

    And you will be able to see all the available images on your system.

    explorejava 10.1 936181cf1c9e About a minute ago 883MB
  10. Run your explorejava image by using image id corresponding it.
    docker run 936181cf1c9e

    Resulting in the following output

    Welcome to Java 10! Enjoy exploring!
  11. Now open a new tab and check for available containers using the following command. Make sure you are in your jdk10docker directory.
    docker ps -a

    Which will result in an output like this

    ca7776ad7811 936181cf1c9e "java JdkTen" About a minute ago Up About a minute priceless_lamarr
  12. Now execute the container with the corresponding container id.
    docker exec -it ca7776ad7811 bash

And Java 10 is ready to use.
You can simply compile and run your java class or you can use Java REPL just by typing jshell.

For instance,

root@2228b1cf6513:/# jshell

jshell>  var list = List.of(1,"Java 10",12.3)

list ==> [1, Java 10, 12.3]

Hope you liked the blog. Thanks for reading!




Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s