295

How do we run a jar file in command prompt?

Community
  • 1
  • 1
Nirav
  • 5,450
  • 4
  • 32
  • 44

4 Answers4

511

Try this

java -jar <jar-file-name>.jar
Bala R
  • 101,930
  • 22
  • 186
  • 204
  • 14
    this 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
  • 1
    as 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
249

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
  • 8,224
  • 2
  • 21
  • 33
  • 2
    This 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
  • 5
    For 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
  • 3
    I 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
35

java [any other JVM options you need to give it] -jar foo.jar

QuantumMechanic
  • 13,239
  • 3
  • 38
  • 65
21

You can run a JAR file from the command line like this:

java -jar myJARFile.jar
icktoofay
  • 117,602
  • 18
  • 233
  • 223