1

Is it possible to execute a jar file by simply typing the jar name into the command line? Is there anything I can add to the manifest to achieve this?

Ie. jarname.jar instead of java -jar jarname.jar

Jarrod Cabalzar
  • 408
  • 1
  • 5
  • 24
  • 2
    well, you can wrap your jar into some script. What OS is it? – Leo Feb 24 '14 at 01:14
  • 2
    I agree that writing a .sh or .bat file would be the simplest and best approach – James Black Feb 24 '14 at 01:16
  • 1
    Create a `Batch File` if using `Windows` or use a `shell script` for unix machines :-) Example (batch file) `@ECHO OFFset /p fileName="Enter JAR to execute" java -jar %fileName%.jar`. That's it now double click this batch file and run the jar. – nIcE cOw Feb 24 '14 at 01:16
  • @nIcEcOw If you make an executable jar you can run it just by double-clicking the jar itself. – Jason C Feb 24 '14 at 01:28
  • @JasonC : LOL, too true. I was just thinking about this very thingy, as to __WHAT WAS I THINKING, while commenting on the question?__ :-) – nIcE cOw Feb 24 '14 at 01:32
  • 2
    If you want an cross-platform example startup script, I've grabbed the outputs from Gradle (a build tool) when it packages things. They are made to work in most circumstances and therefore are very long, so I put them up as a gist: https://gist.github.com/flaviut/9180497 – flaviut Feb 24 '14 at 01:50

1 Answers1

2

You can't really avoid that easily.

A common strategy is to deploy an executable startup script with your application; one per platform since this is platform specific (e.g. an executable shell script for Linux, and a batch script for Windows). Standard shell scripting techniques can be used to pass command line parameters to the script on to your application.

Note that there are some clever platform-specific tricks, such as the Linux one here (and I'm not certain but it may be possible to make all jars executable on the command line with some dirty registry hacks on Windows); but if you are going for platform-independence (and you don't want to have to post-process your JARs after a build), deploying a wrapper script is the way to go.

Community
  • 1
  • 1
Jason C
  • 38,729
  • 14
  • 126
  • 182