Dev Learning Lab

The labs on this site need JavaScript to run. The same content is written out below.

Fundamentals › Architecture and operations

Deployment

When does a running server change after you edit the code?

Why it exists — A Java server does not run source files as they are. It compiles the source files into a jar file, and the process that ran that jar with java -jar answers requests. So even after you edit the code, building a new jar and stopping and starting the server are still left to do. If you do these by hand each time, it is easy to forget one or for the order to differ from person to person, and it also becomes unclear which version is running on the server now. If the deployment steps are decided in advance and followed exactly, the new version is put on the server in the same order every time, so forgotten steps decrease, and you can tell which version has been running since when.

Where it's used today — Deployment happens each time a new feature is released or a bug is fixed. A Spring Boot project usually builds a jar file with ./gradlew bootJar (./mvnw package for Maven) and runs it on the server with java -jar. These days it is common to set things up so that when code is pushed to a repository, a tool such as GitHub Actions or Jenkins goes on through the build and the tests to putting it on the server. If there are several servers, they are replaced one at a time, so that other servers keep receiving requests while one is restarted.

A Spring Boot server does not run source files (.java) as they are. It compiles the source files into .class files, bundles them into one jar file, and runs that jar file with the java -jar command. When it runs, one process (a running program) is created, and when this process starts, it reads in the classes from the jar file and answers requests with that code. So the code is in three separate places: the source file that a person edits, the jar file made by the build, and the running server process. Even if you edit the source file, the jar file does not change by itself, and even if you build a new jar file, the process that is already running does not change. Compiling and building the jar file is done by the ./gradlew bootJar command (the bootJar task provided by the Spring Boot Gradle plugin), and only after you stop the running server and start it again with java -jar does the new code answer requests. Making a new version really run like this is called deployment. The first part of the code box is two Java files, and the second part is what is done in the terminal (lines that start with # are numbered to-do items, and the lines under them are the commands you type in the terminal). In this lab you change "Version 1" to "Version 2" in HelloController, a class we made as an example, then remove or reorder building a new jar file and stopping and starting the server, and you see which version answers the request.

Checked against real execution · Ran the code on the screen as it is in 12 combinations on JDK 21 and Spring Boot 4.1.1 and checked against the results: compiled the Java files with javac and built app.jar with the jar command (instead of ./gradlew bootJar; that bootJar does the same job was checked in the official Spring Boot and Gradle documentation), started and stopped the server with java -jar, sent requests with curl, and recorded the response, the process that had port 8080 open, and the values in the jar and the source

Concepts in this lab

Deployment
Deployment is the work of putting a new version of the code into a server that is really running, so that users' requests reach the new version. A Java server runs a jar file made by compiling the source, not the source files themselves, and a server that is already running runs on the code it read in when it started. So editing and saving a source file is not enough to make a deployment; you have to build a new jar file from the edited source, and then the server has to be started again with that jar. The simplest way is to stop the server and start it again with the new jar, and with this way, when there is only one server, requests fail while it is stopped. The Kubernetes documentation explains this work as replacing old Pods with new Pods, and it distinguishes Recreate, which ends all the existing Pods and then creates new ones, from RollingUpdate, which replaces them gradually.
Server restart
Where it's used today — A Spring Boot server usually reads the settings file when it starts, so you have to start it again even after editing application.properties. During development, Spring Boot DevTools restarts the application automatically every time a file on the class path changes (while leaving the JVM process as it is).
A server restart is ending a running server process and running the same server program again to create a new process. For a Spring Boot server, you end the process started with java -jar (with Ctrl+C in that terminal, for example) and start it again with java -jar. The code the ended process had read in disappears with it, and when the new process starts, it reads in the classes from the beginning from the jar file that is on the disk at that moment. So if you restart without building a new jar file, it only reads the old code again. Until it is stopped and started again, no process accepts connections, so a request that arrives in between fails with a connection refused error.
Executable JAR
Why it exists — To run a server, you used to have to write my classes and dozens of library jars on the class path and type the java command, and it was also troublesome to match the libraries on each server. An executable jar gathers what is needed into one file, so that if the server has a JDK, you can start it with java -jar.
Where it's used today — This is the most common form when deploying a Spring Boot server. A CI tool puts the jar built with ./gradlew bootJar or ./mvnw package on the server and starts it with java -jar, and when building a container image, too, it is common to put this jar in the image and write java -jar as the run command.
An executable jar is a jar file made so that it can be run directly with the single command java -jar. The jar that the bootJar task of Spring Boot builds has my classes (BOOT-INF/classes), the dependency libraries such as Spring and Tomcat (BOOT-INF/lib), and a record of which class's main to run first, so if you move just this one file to a server, you can start even the web server with java -jar. An executable jar is the code at the moment the source was compiled. If you edit the source file and do not build the jar again, the jar stays as the old code, and even if you build the jar again, a process that is already running from that jar keeps running on the code it read in when it started.
./gradlew bootJar
Where it's used today — In a Maven project that uses the Maven plugin of Spring Boot, ./mvnw package builds an executable jar. To start the application right away during development without building a jar, use ./gradlew bootRun.
./gradlew bootJar is the command that builds an executable jar in a Spring Boot project built with Gradle. ./gradlew is the Gradle run script (the Gradle wrapper) kept in the project folder, and bootJar is a task provided by the Gradle plugin of Spring Boot. This task first compiles the source files, then gathers those classes and the dependency libraries and builds the jar file in the build/libs folder (the name is usually project-name-version.jar). If you type ./gradlew build, bootJar runs together with the tests. This command only builds the jar file; it does not change a server that is already running or start it again.
Programs and processes
A program is a file stored on disk, and a process is that file being run. In Java, Counter.class made by javac is a program file, and the command java Counter makes the operating system create one process that runs that file. Even if there is 1 file, one process is created each time you run it, so running the same file twice gives two processes. A process exists only while it is running, and it disappears when it ends. A process has its own execution environment.
Connection refused error
A connection refused error is the error that the sender gets when it asks for a TCP connection but no program has that port open and is waiting. In Java, a java.net.ConnectException comes at the line new Socket, and in the check run on Windows 11 the message was "Connection refused: connect". The API documentation of ConnectException says that this exception is an error that occurred while connecting, and that it is usually a case where the connection was refused because no process is waiting at the remote address and port. The operating system of that computer refuses, and inside the same computer the error comes right away, without waiting. Because the connection was not opened, not a single piece of text is sent. The TCP standard says to send a reset signal (RST) to a request that comes with no connection (RFC 9293 section 3.5.2).

Try these

Running with the default settings

  1. Step 1: ./gradlew bootJar compiles the source files into .class files and bundles them into the jar file build/libs/app.jar. The HelloController in this jar file is code that returns "Version 1".
  2. Step 1: java -jar build/libs/app.jar runs the jar file and creates one process. When it starts, this process reads in the classes from the jar file, and Spring Boot starts the web server, which waits for requests on port 8080. Now it answers GET /hello with "Version 1".
  3. Step 2: a person opens HelloController.java, changes "Version 1" to "Version 2", and saves it. The only thing that changed is the one source file on the disk. The jar file is still "Version 1", and the running server still answers "Version 1". Saving the source file does not make compiling and building the jar happen by itself.
  4. Step 3: ./gradlew bootJar builds the jar file again from the current source file. Now the code in the jar file returns "Version 2". But the server was left running, so even when a request was sent at this point, the running process answered "Version 1". A process only runs on the classes it read in when it started, and it does not pick up that the jar file on the disk changed.
  5. Step 4: press Ctrl+C in the terminal where you started the server to stop it. When the process ends, the code it had read in disappears with it. The source file and the jar file on the disk stay as they are.
  6. Step 5: java -jar build/libs/app.jar starts it again. A new process is created, and when it starts, it reads in the classes from the beginning from the jar file that is on the disk now. The code in that jar file returns "Version 2". The edited code has now become the code of the running server.
  7. Step 6: in another terminal, curl sends a GET /hello request. It is the same as the request a browser sends when you type localhost:8080/hello in the address bar.
  8. The running process answers "Version 2" with its own code, and curl receives "Version 2". The source file, the jar file, and the code of the running server are all the same, "Version 2". The edited new version answered the request.

Result of this run

Code

// File: src/main/java/com/example/demo/DemoApplication.java
// DemoApplication and HelloController are classes made as examples for this lab
// @SpringBootApplication, SpringApplication, @RestController, and @GetMapping are provided by Spring Boot and Spring
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {
    // When the jar file is run with java -jar, this main is called, and Spring Boot starts the web server
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

// File: src/main/java/com/example/demo/HelloController.java
package com.example.demo;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    // When a GET /hello request arrives, Spring calls this method, and the text it returns becomes the response
    @GetMapping("/hello")
    public String hello() {
        return "Version 1"; // Changed to "Version 2" in step 2 below
    }
}

# What you do in the terminal (in the project folder; the jar name is shortened to app.jar)
# 1. Build the jar file and start the server with that jar
./gradlew bootJar
java -jar build/libs/app.jar
# 2. Change "Version 1" to "Version 2" in HelloController.java and save it
# 3. Build the jar file again (with the server left running)
./gradlew bootJar
# 4. Stop the running server (Ctrl+C in the terminal where you started it)
# 5. Start it again with the newly built jar
java -jar build/libs/app.jar
# 6. Send a request from another terminal
curl http://localhost:8080/hello

Assumptions of this model

The environment was JDK 21.0.12.1, Spring Boot 4.1.1 (Spring Framework 7.0.9, embedded Tomcat 11.0.24), curl 8.21.0, and Windows 11. For each of the 12 combinations, I took the code on the screen as it is from the code function of this lab, wrote the two Java files in the first part out as files, and really did the to-do items and commands in the second part line by line. java -jar build/libs/app.jar and curl http://localhost:8080/hello were run exactly as the commands on the screen, and the server started on the default port 8080. I did not run ./gradlew bootJar, because it needs to download the Gradle plugin, and I did the same job with JDK commands. I compiled src/main/java with javac and made build/libs/app.jar with the jar command; unlike bootJar, which puts the dependency libraries into BOOT-INF/lib of the jar file, this jar pointed to the library jar files with the Class-Path of the manifest. On Windows, the jar command could not newly write the app.jar that the running java process had open (an error that another process is using the file), so in the six combinations that build the jar while the server is left running, I made the new jar under another name and then overwrote the contents of app.jar in place. Stopping the server was done by forcibly ending the process, not by Ctrl+C. At each request I recorded the output of curl, the process ID that had port 8080 open at that time (netstat), the text returned by the HelloController in app.jar read with javap, and the text written in the source file on the disk, and checked them against the result table. I also checked the answer of the running server right after the jar was built again, the answer of the server just before it was stopped, the number of times it was started, the number of times the jar was built, and, for the combination that requested while the server was stopped, the answer after it was started again, and all 12 combinations matched (verify/net/cmp_deploy_basic.js). The connection refused error is what curl reported with exit code 7 (Failed to connect … Could not connect to server). What I checked against documentation is Packaging Executable Archives in the Spring Boot Gradle plugin documentation (the description that the bootJar task builds an executable jar, puts the classes and dependency libraries into BOOT-INF/classes and BOOT-INF/lib, and that the jar can be run with java -jar), Running Your Application in the Spring Boot reference (the description that an executable jar is run with java -jar), the Java Plugin in the Gradle documentation (the description that the jar task depends on classes, and classes depends on compileJava), and the Base Plugin (the description that the jar is created in build/libs by default and its name is decided by the project name and the version). The app.jar on the screen is a shortened name. Not covered: the behavior when you overwrite the jar of a running server on a Linux server, the cleanup work Spring Boot does when you stop it with Ctrl+C, and Spring Boot DevTools, which restarts the application by itself when a file changes, were not run in this lab. How to remove the gap while a server is stopped and started (rolling several machines one after another, blue-green, which builds a whole new environment, and so on) is covered in the in-depth lab "Deployment strategies", the automation that builds, tests, and puts the edited code on a server is covered in "CI/CD pipelines", and the way that Kubernetes replaces servers packed in containers is covered in "Kubernetes self-healing".

Previous lab: Message queues · Next lab: Git commits and branches

Concepts in this lab

What this lab shows

Settings

Code


      
    

Diagram

Run comparison

Try these