Exploring Java 10 using Docker

Knoldus Inc.
Knoldus - Technical Insights
3 min readMay 2, 2018

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.
    [code language = “bash”]
    mkdir jdk10docker
    cd jdk10docker[/code]
  4. Make a java class JdkTen.
    [code language = “bash”]
    vi JdkTen.java
    [/code]
  5. Add the following code in your java class file.
    [code language = “Java”]
    public class JdkTen {
    public static void main(String[] args) {
    System.out.println(“Welcome to java 10! Enjoy exploring!”);
    while (true) {
    try {
    Thread.sleep(100000);
    } catch (Exception e) {
    }
    }
    }
    }
    [/code]
  6. Make a text file with the name Dockerfile.
    [code language = “bash”]
    vi Dockerfile
    [/code]
  7. Copy following code in the Dockerfile.
    [code language = “bash”]
    #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”]
    [/code]
  8. Build docker image. It may take some time to download.
    [code language = “bash”]
    docker build -t explorejava:10.1 ~/jdk10docker
    [/code]
    Which should result in the following output:
    [code language = “bash”]
    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
    [/code]
  9. Check for all the available docker images in your system using the following command:
    [code language = “bash”]
    docker images
    [/code]
    And you will be able to see all the available images on your system.
    [code language = “bash”]
    REPOSITORY TAG IMAGE ID CREATED SIZE
    explorejava 10.1 936181cf1c9e About a minute ago 883MB
    [/code]
  10. Run your explorejava image by using image id corresponding it.
    [code language = “bash”]
    docker run 936181cf1c9e
    [/code]
    Resulting in the following output
    [code language = “bash”]
    Welcome to Java 10! Enjoy exploring!
    [/code]
  11. Now open a new tab and check for available containers using the following command. Make sure you are in your jdk10docker directory.
    [code language = “bash”]
    docker ps -a
    [/code]
    Which will result in an output like this
    [code language = “bash”]
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    ca7776ad7811 936181cf1c9e “java JdkTen” About a minute ago Up About a minute priceless_lamarr
    [/code]
  12. Now execute the container with the corresponding container id.
    [code language = “bash”]
    docker exec -it ca7776ad7811 bash
    [/code]

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,

[code language = “bash”]

root@2228b1cf6513:/# jshell

[/code]

[code language = “bash”]

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

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

[/code]


Hope you liked the blog. Thanks for reading!


References:
docs.docker.com
docs.oracle.com

knoldus-advt-sticker

--

--

Knoldus Inc.
Knoldus - Technical Insights

Group of smart Engineers with a Product mindset who partner with your business to drive competitive advantage | www.knoldus.com