Detailed explanation of how to use Docker to build a simple Java development and compilation environment

Detailed explanation of how to use Docker to build a simple Java development and compilation environment

There are many versions of the Java language. In addition to the commonly used Java 8, some legacy projects may use Java 7, and some newer projects may use Java 10 or later. If you want to switch your local Java development environment, it will take some time, and you will have to do it every time you switch between different versions in the future.

The emergence of Docker makes it easy for us to maintain different versions of development and compilation environments. If you don’t know what Docker is, you can read the Docker Introduction. There are two ways to build a Java development environment. One is to compile and run in the container, and the other is to compile and run outside the container. Let's take a look at how to do it respectively.

Preparation: First, make sure Docker is installed. If it is convenient, you can pull down openjdk:8 image in advance to save everyone's time. This article has been verified in the environment of macOs 10.15.7 and Docker 19.03.8.

Compile and run in container

This article takes a simple Helloworld program as an example. The file directory structure and code of the project are as follows.

$ ls -lh
total 24
-rw-r--r-- 1 shiqiang staff 60B 11 11 19:02 Dockerfile
-rw-r--r-- 1 shiqiang staff 123B 11 11 19:02 Helloworld.java

The content of the code.

public class HelloWorld {
 public static void main(String args[]) {
 System.out.println("Hello world.\n");
 }
}

After completing the above preparations, you can edit the contents of the Dockerfile.

FROM openjdk:8 #Build an image based on openjdk:8 COPY . /usr/src/myapp #Copy the code in the directory to the location of /usr/src/myapp in the image WORKDIR /usr/src/myapp #Set /usr/src/myapp as the working directory RUN javac Helloworld.java #Run the compilation command to compile the Helloworld.java program CMD ["java", "Helloworld"] #Execute the compiled Helloworld

Build the image file.

$ docker build -t java_in_docker_env_8 . 
$ docker images # Use the docker images command to see the built java_in_docker_env_8 image

Run the container.

$ docker run -it --rm java_in_docker_env_8
Hello world.

$ ls -lh     
total 24
-rw-r--r-- 1 shiqiang staff 112B 11 12 10:53 Dockerfile
-rw-r--r-- 1 shiqiang staff 123B 11 12 10:49 Helloworld.java

You can see the output of the program and see whether there are any uncompiled results in the current directory. However, this method requires compilation every time the container is started, and there is no way to share the compilation results. The next section describes how to save the compilation results outside the container.

Compile and run outside the container

The working directory and code preparation are the same as in the previous section, but the Dockerfile file is slightly different from the previous section.

FROM openjdk:8
COPY . /usr/src/myapp
WORKDIR /usr/src/myapp

Build the image file.

$ docker build -t java_env_8
$ docker images # Use the docker images command to see the built java_env_8 image

Compile the file.

$ docker run --rm -v "$PWD":/usr/src/myapp -w /usr/src/myapp java_env_8 javac Helloworld.java
$ ls -lh # You can see the compiled result total 32
-rw-r--r-- 1 shiqiang staff 112B 11 12 10:53 Dockerfile
-rw-r--r-- 1 shiqiang staff 427B 11 12 11:09 Helloworld.class
-rw-r--r-- 1 shiqiang staff 123B 11 12 10:49 Helloworld.java

Run the program.

$ docker run --rm -v "$PWD":/usr/src/myapp -w /usr/src/myapp java_env_8 java Helloworld 
Hello world.

Final Thoughts

This article only builds a simple Java development environment. The advantage of using this method to compile and run Java programs is that there is no need to install JDK locally, which is more convenient when you need to compile programs for multiple JDK versions. However, complex projects are often built using Maven or Gradle. Can such projects be built using the method mentioned in this article? Please look forward to the next article in this series. You are also welcome to follow the official account to get the latest article push.

ReferencesDocker - Create a Java development environment

This is the end of this article about using Docker to build a simple Java development and compilation environment. For more information about using Docker to build a Java development and compilation environment, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to start a Java program in docker
  • How to develop Java 8 Spring Boot applications in Docker
  • Steps to build a Java environment using Docker
  • How to use Docker, a Java data development auxiliary tool, and ordinary programs

<<:  Comprehensive understanding of line-height and vertical-align

>>:  Detailed explanation of MySQL's FreeList mechanism

Recommend

Vue's global watermark implementation example

Table of contents 1. Create a watermark Js file 2...

Install MySQL 5.7.17 in win10 system

Operating system win10 MySQL is the 64-bit zip de...

Detailed explanation of anonymous slots and named slots in Vue

Table of contents 1. Anonymous slots 2. Named slo...

Docker Compose practice and summary

Docker Compose can realize the orchestration of D...

The latest collection of 18 green style web design works

Toy Story 3 Online Marketing Website Zen Mobile I...

Introduction to local components in Vue

In Vue, we can define (register) local components...

Summary of Nginx location and proxy_pass path configuration issues

Table of contents 1. Basic configuration of Nginx...

How to develop Java 8 Spring Boot applications in Docker

In this article, I will show you how to develop a...

Common failures and reasons for mysql connection failure

=================================================...

Theory: The two years of user experience

<br />It has been no more than two years sin...

Detailed explanation of Mysql function call optimization

Table of contents Function call optimization Func...

Shtml Concise Tutorial

Shtml and asp are similar. In files named shtml, s...

HTML Tutorial: Collection of commonly used HTML tags (5)

These introduced HTML tags do not necessarily ful...

Why is it not recommended to use index as key in react?

1. Compare the old virtual DOM with the new virtu...