---
title: Note on Running the Main Class of a Maven Project Directly with the java Command
tags: ["Maven", "Java", "Spring Boot"]
categories: ["Programming", "Java", "Maven"]
date: 2025-07-29T01:34:48Z
updated: 2025-07-29T01:40:12Z
---

> ⚠️ This article was automatically translated by OpenAI API (ggml-org/gpt-oss-120b-GGUF).
> It may be edited eventually, but please be aware that it may contain incorrect information at this time.

To run a class that contains the `main` method in a Java project created with Maven, we often use the `mvn exec:java` command, `mvn spring-boot:run`, or create an executable JAR and run it with `java -jar`. However, I discovered that it can also be run directly with the `java` command, so here's a note.

After `mvn compile`,

```
java -cp target/classes:$(mvn dependency:build-classpath -q -Dmdep.outputFile=/dev/stdout) com.example.changeme.Main
```

Replace the `com.example.changeme.Main` part with your own class.

The point is that the `mvn dependency:build-classpath -q -Dmdep.outputFile=/dev/stdout` command can output all the JAR paths it references in one go.

Let's try it with a Spring Boot sample.

```bash
curl -s https://start.spring.io/starter.tgz \
       -d artifactId=demo \
       -d name=demo \
       -d baseDir=demo \
       -d packageName=com.example \
       -d type=maven-project \
       -d applicationName=DemoApplication | tar -xzvf -
cd demo
./mvnw compile
java -cp target/classes:$(./mvnw dependency:build-classpath -q -Dmdep.outputFile=/dev/stdout) com.example.DemoApplication
```

The Spring Boot application starts correctly.

```
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

 :: Spring Boot ::                (v3.5.4)

2025-07-29T10:34:00.953+09:00  INFO 94084 --- [demo] [           main] com.example.DemoApplication              : Starting DemoApplication using Java 21 with PID 94084 (/private/tmp/demo/target/classes started by tmaki in /private/tmp/demo)
2025-07-29T10:34:00.954+09:00  INFO 94084 --- [demo] [           main] com.example.DemoApplication              : No active profile set, falling back to 1 default profile: "default"
2025-07-29T10:34:01.137+09:00  INFO 94084 --- [demo] [           main] com.example.DemoApplication              : Started DemoApplication in 0.347 seconds (process running for 0.499)
```

If you run it repeatedly, you can save the build‑classpath information in advance,

```bash
./mvnw dependency:build-classpath -q -Dmdep.outputFile=/dev/stdout > build-classpath.txt
```

Referencing it at runtime speeds up the startup.

```bash
java -cp target/classes:$(cat build-classpath.txt) com.example.DemoApplication
```

If there are changes to `pom.xml`, don't forget to update `build-classpath.txt`.
