> For the complete documentation index, see [llms.txt](https://jrp.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jrp.gitbook.io/notes/java/running-java-applications.md).

# Running Java Applications

While running Java program using command line/server, I always got confused about using -jar option and -cp option. We bundle the application in jar file using maven/gradle or any building tool. Now there may be some resource files which should be provided from outside of the application. This is very common scenario.

Now if you use

```
java -cp /path/to/resources -jar app.jar
```

Java won't read classpath resources and will look for classpath mentioned in app.jar manifest file.

So, in such case,

```
java -cp /path/to/resources:/path/to/jar package.main-classname
```

Now, Java will search for the jar file and resources in classpath (separated by ; in windows and : in unix).

if you are not providing any -cp option, java checks System's CLASSPATH and uses the directories.

If there is no envrionment variable CLASSPATH set, then it checks the current directory

```
java -cp . package.main-classname
```

Any arguments provided after the main class name will be considered as program arguments

```
java -cp /path/to/all/resources package.main-classname arg1 arg2 arg3
```

You can pass JVM arguments before the main class and after -cp option with -D as prefix

```
java -cp /path/to/resources -Dkey=value package.main-classname arg1 arg2
```

Have a nice day :)
