Build and packaging: Java and C++

A C++ program is compiled into machine code for one platform, linked against a runtime library, and run by the operating system. A Java program is compiled into bytecode for the JVM, packed into a jar with a manifest, and run by a launcher that reads the manifest. Both are one file to ship; what the file needs in order to start is the difference this page runs.

The task

A program with a nested class, so the archive has more than one class file, compiled into out, packed into app.jar with a main class, run from the jar with two arguments, and listed. A C++ version of the same program is built by a Makefile that produces one executable, and the page shows that file without running it.

The Java version

public class App { static class Greeting { static String text() { return "hello from a jar"; } } public static void main(String[] args) { System.out.println(Greeting.text()); System.out.println("args: " + args.length); } }

javac -Xlint:all -d out App.java
java -cp out App one two

hello from a jar
args: 2

The class path run is the one the course uses everywhere: the folder holds the class files and the launcher is told where the root is. The nested class compiles to a second file, App$Greeting.class, which is the dollar of the class-file naming and not a package. A jar has to carry both.

The jar and its manifest

jar --create writes a zip of the compiled folder, and --main-class App puts the one manifest line the launcher needs. The -C out . changes into the folder before adding, so the classes sit at the root of the archive. The three commands pack, run and list:

jar --create --file app.jar --main-class App -C out .
java -jar app.jar one two
jar --list --file app.jar

hello from a jar
args: 2
META-INF/
META-INF/MANIFEST.MF
App$Greeting.class
App.class

The jar is 1365 bytes: two class files, the manifest and the zip directory. The manifest holds Manifest-Version, Created-By and Main-Class: App, and it is the only part of the archive a person reads. The class files are bytecode for a JVM of a given class-file version, which is why a jar built with Java 21 does not run on a Java 17 without being rebuilt for it.

The C++ version

The C++ program is one source and one executable, and the build is two rules in a Makefile. Neither block is compiled here, because the image has no g++ or make; the text after them says what the build produces.

#include <iostream> #include <string> std::string text() { return "hello from an executable"; } int main(int argc, char** argv) { std::cout << text() << '\n'; std::cout << "args: " << (argc - 1) << '\n'; }

app: main.cpp g++ -std=c++23 -Wall -Wextra -O2 main.cpp -o app clean: rm -f app

make would run the compiler and produce app, an ELF file that the kernel can load: it holds the machine code, the strings, the relocation tables and a dependency on libstdc++ and libc. Running it needs no compiler and no virtual machine, and the file does not run on a different architecture or against an incompatible libstdc++. The Java jar needs a JVM of the right version on every machine and runs unchanged on any architecture that has one.

What differs and why

The C++ compiler resolves everything it can at build time: the functions are in the binary, the types are gone, the linker has picked the addresses. The Java compiler resolves names against a class path but leaves the calls symbolic: a invokevirtual names the method and the JVM resolves it at run time, which is what allows a class to be replaced and a program to start without all of its code loaded. The class-file format is the contract between the compiler and the JVM, and the manifest is the one extra line that makes a jar runnable.

A build tool adds three things to both: dependency resolution, a target graph and reproducibility. make gives the second with timestamps; Maven and Gradle give all three with a repository and a lock of versions. The Java course shows the two commands of the JDK in lesson 126 and the Maven build that this image cannot run in lesson 128.

The deployment story follows. A C++ program ships as an executable per platform, often statically linked to avoid the libstdc++ question. A Java service ships as a jar and a JVM, which is why a container image for Java is a base image with a JRE and one java -jar line. Neither is free: the first multiplies the builds by the platforms, the second pays for the JVM's memory and startup.

The habit that breaks

The habit is to treat the class file as the executable. A C++ programmer copies App.class to another machine and expects it to run, and the answer is no main manifest attribute at best and a missing class at worst. The unit of delivery is the jar, and the unit of execution is the launcher plus the jar plus a JVM: three things, and the third is not in the archive.

The second habit is to put the manifest together by hand and forget the blank line at its end. A manifest is a sequence of Name: value lines in UTF-8, and a malformed one makes the launcher ignore the main class without saying why. The --main-class flag exists so that nobody has to write those lines, and the page's program is runnable because of one of them.

The next page

The next page is threads: a Java thread over a shared object against std::thread, the data race that Java defines and C++ calls undefined, and the two tools each language gives for the shared state. The C++ lesson is make, and the Java lesson is jar and javadoc.

The Java program was compiled with OpenJDK 21.0.13 (javac), packed with jar and run on Alpine Linux in a Docker container. The C++ program and the Makefile are shown and were not built here: the image has no g++ or make.

Article author: Arthur Isaev