How do we run a jar file in command prompt?
Asked
Active
Viewed 9.5e+01k times
4 Answers
553
Try this
java -jar <jar-file-name>.jar

Bala R
- 107,317
- 23
- 199
- 210
-
19this will work when the created jar file is a runnable jar. – Chetan Feb 23 '15 at 12:09
-
More times when you usea a '.jar', not work correctly and you need call with a interal sentence. Thanks :) – Jun 01 '17 at 08:16
-
1as a note, if your application uses a web framework there maybe more params that you have to pass in, for example with dropwizard... `java -jar myapp.jar server xxx.yml` – Opentuned Aug 16 '17 at 11:40
-
Using these command you can run the jar file using the background process. **nohup java -jar /web/server.jar &** – Buddhika Lakshan Nov 22 '17 at 06:59
261
If you dont have an entry point defined in your manifest invoking java -jar foo.jar
will not work.
Use this command if you dont have a manifest or to run a different main class than the one specified in the manifest:
java -cp foo.jar full.package.name.ClassName
See also instructions on how to create a manifest with an entry point: https://docs.oracle.com/javase/tutorial/deployment/jar/appman.html

Lynch
- 9,174
- 2
- 23
- 34
-
2This also works if you want to run a different class main than what is specified in the manifest. – br3nt Jan 13 '16 at 01:30
-
6For Developers, I think this is a better answer than Bala R's, although they should ideally be combined – Hack5 Aug 08 '16 at 18:23
-
3I would say this should be the accepted answer not the other one. – Indrajeet Gour Aug 09 '18 at 17:23
-
what if i don't have an main method ? i want to use the a static method , with parameters – MoxGeek Oct 09 '18 at 11:47
-
@MoxGeek you need a static method named `main`. I don't think there is a way arround this. You can still write your own `Main` class and import your other class from there. Extra parameters to pass to the main method just comes after your `full.package.name.ClassName`. You could also have a look to java REPL, like jshell and import what you need, this solution would feel more "scripting" style. – Lynch Oct 09 '18 at 15:15
26
You can run a JAR file from the command line like this:
java -jar myJARFile.jar

icktoofay
- 126,289
- 21
- 250
- 231