How to use the java program as a command in cmd?


I have a Java program utility that I want to execute as a command in cmd. I added the location to the PATH variable, but java programs needs to be executed using java - jar "...". How do I shorten that to just the program name, like mysql or netstat?

Update: I neglected to mention that this java program takes arguments of its own to handle its tasks, so the batch program would need to pass the arguments passed to it over the the java program. I'm not skilled enough in batch to know how to do this.


You could create a batch file or bash script (depending upon your OS) that calls the program with the proper java -jar commands, and simply name the batch (or bash) script whatever you would like to enter as the command. Place this in a directory that is in your PATH variable, and have at it.

Edit: Read this for info on how to parse command line parameters in batch scripts. Just take the parameters passed to the batch file, parse them, and pass them to your jar file with:

java -jar jarfile.jar param1 param2 ...

So for example, lets's assume that your program takes two arguments. Your script could then be as follows:

java -jar jarfile.jar %1 %2

I am not an expert in batch files by any means, so there is probably a more proper way to do this. That being said, why over complicate things?