Build tools and deployment: C# and Java
A C# project is a file that the SDK reads, and the SDK is the compiler, the project system and the packager in one command. A Java program needs no project file at all: javac takes the source files, java takes the class files, and a build tool is a separate choice, of which Maven is the oldest that is still common. This page runs the four paths in this container and measures them.
C# and the SDK
The project file of the records program is eight lines: the SDK, the output type, the target framework and three settings. Everything else the build needs is in the SDK:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AssemblyName>Program</AssemblyName>
<NuGetAudit>false</NuGetAudit>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
</PropertyGroup>
</Project>
A Release build in this container takes about three seconds and prints the summary:
dotnet build -c Release -nologo -v:q
echo "status: $?"
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:02.63
status: 0
dotnet publish writes a folder that runs on a machine with the .NET runtime installed. The folder of this program is 128 KiB, and most of it is the dependencies file and the runtime configuration that the host reads:
du -sh publish
ls publish
128K publish
Program
Program.deps.json
Program.dll
Program.pdb
Program.runtimeconfig.json
Java and javac
The Java program is one file, and the two commands are the compiler and the launcher. -d out puts the class file in a folder, which is the closest thing Java has to an output directory:
javac -d out Records.java
jar --create --file app.jar --main-class Records -C out .
java -jar app.jar
echo "status: $?"
records 1.0
Point[x=0, y=0]
Point[x=3, y=0]
equal true
same hash true
a circle of radius 2.0
a square of side 3.0
a point
status: 0
The jar is 4 556 bytes, and it runs with java -jar because the manifest names the main class. Since Java 11 a single source file can also be compiled and run in one command, which is the shortest path from a file to a result:
java Records.java
echo "status: $?"
records 1.0
Point[x=0, y=0]
Point[x=3, y=0]
equal true
same hash true
a circle of radius 2.0
a square of side 3.0
a point
status: 0
Maven
A Java project with more than one file usually has a build tool, and Maven is the one in this container. Its project file is XML, its layout is fixed (src/main/java), and the command is mvn package:
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>records</artifactId>
<version>1.0.0</version>
<properties>
<maven.compiler.release>21</maven.compiler.release>
</properties>
</project>
mvn -q -B package
echo "status: $?"
status: 0
The command produced target/records-1.0.0.jar, 1 683 bytes, and took 20.2 seconds the first time: the plugins of Maven are themselves downloaded from a repository, and a machine without a network cannot run a Maven build that has never run before.
What differs
- A C# project names its target framework and the SDK provides everything else; a Java project names its dependencies and a build tool provides the rest. Both are one file, and the C# one is smaller because the SDK is installed with the compiler.
- The C# publish folder depends on the runtime being installed. A self-contained publish carries it: the C# course measures one at about 24 MB, trimmed, in lesson 112. A jar depends on the JRE in the same way, and a jlink image or a jpackage bundle is the Java answer to the self-contained folder.
- java File.java is the closest thing either language has to a script: no project, no output folder, no jar. C# has no equivalent and needs a project file even for one line.
- Both ecosystems have a package manager and a lock file: NuGet with packages.lock.json and Maven with the versions in the pom. A build that does not pin them can change under the project.
- The deployment story of a web service is the same shape in both: a publish or a jar, a runtime, and a process to start. What differs is the size of the runtime and the tools around it, which the closing page weighs.
The next pages measure the two runtimes: the same loop, a million records, and the time to print one line.
Exercises
- Publish the C# program self-contained and compare the size with the 128 KiB folder.
- Run the Java program with java Records.java and compare the output with the jar.
- Add a second class to the Maven project and build it.
- Explain what a machine without the .NET runtime and without the JRE needs to run each program.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does a csproj name that a Java source file does not?
Answer
The target framework and the output type
The page shows the eight-line project file.
-
How large was the publish folder of the C# program?
Answer
128 KiB
The measured du output is on the page.
-
What does this program print?
Console.WriteLine("hello 1.0");Answer
hello 1.0
That is the hello program of the startup page.
-
How large was the jar of the records program?
Answer
4 556 bytes
The page quotes the size of the jar it built.
-
What does java File.java do since Java 11?
Answer
It compiles and runs the single source file
The page runs the records program that way.
-
Why did the first Maven build take 20.2 seconds?
Answer
Maven downloaded its plugins, which it needs for a build it has never run
The page names the download as the cost of the first build.
-
What does jpackage or jlink give a Java program?
Answer
A bundle that does not need the JRE installed, like a self-contained publish
The page compares it with the self-contained publish.
-
What does this program print?
Console.WriteLine("Release");Answer
Release
The build command of the page is a Release build.
-
Which build is the C# publish of the page?
Answer
Framework-dependent: it needs the .NET runtime
The folder of 128 KiB holds no runtime.
-
What do both ecosystems pin in a lock file?
Answer
The versions of the packages a build uses
A build that does not pin them can change under the project.
The C# program was built with the .NET SDK 10.0.303 and the Java program with OpenJDK 21.0.13.1 (javac 21.0.13.1), both run on Alpine Linux 3.24.3 (x86_64, four processors) in a Docker container.
Article author: Arthur Isaev