Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

Typically, every meaningful application includes one or more JAR files as dependencies. But there are times a JAR file itself represents a standalone application or a web application.

Here we’ll focus on the standalone application scenario. From now on, we’ll refer to it as a JAR application.

In this tutorial, we’ll first learn how to create a JAR application. Later, we’ll learn how to run a JAR application with or without command-line arguments.

Further reading:

Run JUnit Test Cases From the Command Line

Learn how to run JUnit 5 tests directly from the command line with and without Maven

Command-Line Arguments in Java

Explore how to configure your Java applications using command-line arguments.

Command-Line Arguments in Spring Boot

Learn how to pass arguments from command line into your Spring Boot application

2. Create a JAR Application

A JAR file can contain one or more main classes. Each main class is the entry point of an application. So, a JAR file can theoretically contain more than one application, but it has to contain at least one main class to be able to run.

A JAR file can have one entry point set in its manifest file. In this case, the JAR file is an executable JAR. The main class has to be included in that JAR file.

First, let’s see a quick example of how to compile our classes and create an executable JAR with a manifest file:

$ javac com/baeldung/jarArguments/*.java
$ jar cfm JarExample.jar ../resources/example_manifest.txt com/baeldung/jarArguments/*.class

A nonexecutable JAR is simply a JAR file that doesn’t have a Main-Class defined in the manifest file. As we’ll see later, we can still run a main class that’s contained in the JAR file itself.

Here’s how we would create a nonexecutable JAR without a manifest file:

$ jar cf JarExample2.jar com/baeldung/jarArguments/*.class

3. Java Command-Line Arguments

Just like any application, a JAR application accepts any number of arguments, including zero arguments. It all depends on the application’s need.

This allows the user to specify configuration information when the application is launched.

As a result, the application can avoid hard-coded values, and it still can handle many different use cases.

An argument can contain any alphanumeric characters, unicode characters and possibly some special characters allowed by the shell, for example, @.

Arguments are separated by one or more spaces. If an argument needs to contain spaces, the spaces have to be enclosed between quotes. Either single quotes or double quotes work fine.

Usually, for a typical Java application, when invoking the application, the user enters command-line arguments after the name of the class.

However, that’s not always the case for JAR applications.

As we discussed, the entry point of a Java main class is the main method. The arguments are all Strings and are passed to the main method as a String array.

That said, inside the application, we can convert any element of the String array to other data types, such as char, int, double, their wrapper classes or other appropriate types.

4. Run an Executable JAR with Arguments

Let’s see the basic syntax for running an executable JAR file with arguments:

java -jar jar-file-name [args …]

The executable JAR created earlier is a simple application that just prints out the arguments passed in. We can run it with any number of arguments.

Here’s an example with two arguments:

$ java -jar JarExample.jar "arg 1" arg2@

We’ll see this output in the console:

Hello Baeldung Reader in JarExample!
There are 2 argument(s)!
Argument(1):arg 1
Argument(2):arg2@

So, when invoking an executable JAR, we don’t need to specify the main class name on the command line. We simply add our arguments after the JAR file name. If we do provide a class name after the executable JAR file name, it simply becomes the first argument to the actual main class.

Most times, a JAR application is an executable JAR. An executable JAR can have a maximum of one main class defined in the manifest file.

Consequently, other applications in the same executable JAR file can’t be set in the manifest file, but we can still run them from the command line just like we would for a nonexecutable JAR. We’ll see exactly how in the next section.

5. Run a Nonexecutable JAR with Arguments

To run an application in a nonexecutable JAR file, we have to use -cp option instead of -jar.

We’ll use the -cp option (short for classpath) to specify the JAR file that contains the class file we want to execute:

java -cp jar-file-name main-class-name [args …]

As we can see, in this case, we’ll have to include the main class name in the command line, followed by arguments.

The nonexecutable JAR created earlier contains the same simple application. We can run it with any (including zero) arguments.

Here’s an example with two arguments:

$ java -cp JarExample2.jar com.baeldung.jarArguments.JarExample "arg 1" arg2@

And, just like we saw above, we’ll see this output:

Hello Baeldung Reader in JarExample!
There are 2 argument(s)!
Argument(1):arg 1
Argument(2):arg2@

6. Conclusion

In this article, we learned two ways of running a JAR application on the command line with or without arguments.

We also demonstrated that an argument could contain spaces and special characters (when allowed by the shell).

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW