5

I have a Java executable file, swagger-codegen-cli.jar, that I'd like to be able to execute globally from the console like I would with an .exe in my environment PATH.

Currently if I want to execute it, I have to type java -jar C:\Users\Jacob\swagger-codegen-cli.jar.

How can I "alias" (what's the proper term?) this command globally as something succinct, like swagger-codegen? So something like this:

java -jar C:\Users\Jacob\swagger-codegen-cli.jar generate -i http://localhost/swagger/docs/v1

becomes this:

swagger-codegen generate -i http://localhost/swagger/docs/v1

4

Create a batch file with the following:

java -jar C:\Users\Jacob\swagger-codegen-cli.jar %*

The %* portion tells the batch script to pass all of the parameters it receives to the new command.

If there are no other executables in the same directory as the .jar file, you can safely save the batch file there as swagger-codegen.bat and then add it to your PATH variable.

If there are other executables in the same directory, make sure they don't conflict with any other commands that might be reachable through your PATH variable.

  • 1
    This did the trick, though I had to put my path in quotes even though it has no spaces or odd characters. java -jar C:\Users\Jacob\executables\swagger-codegen-cli.jar gave me the Unable to access jarfile error, but java -jar "C:\Users\Jacob\executables\swagger-codegen-cli.jar" worked just fine – Jacob Stamm Jun 26 '18 at 21:13
1

The DOSKEY command can create an alias in cmd:

doskey swagger-codegen=java -jar C:\Users\Jacob\swagger-codegen-cli.jar

If you need this to be defined whenever cmd starts, you can define it in an initialisation file.

  • Does this support command arguments? i.e. swagger-codegen -i someArg – Jacob Stamm Jun 27 '18 at 3:09
  • @JacobStamm Yes. Add $* to the end of the doskey command. – DavidPostill Jun 27 '18 at 8:20
  • @JacobStamm - The substitution is made in-line, so any extra parameters remain on the command line and are passed to the expanded command. You can add $*, but it's not really needed unless you wand to add extra parameters to the end of the run string. – AFH Jun 27 '18 at 10:56

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.