7

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

1

2 Answers 2

5

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
  • 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 Jun 26, 2018 at 21:13
2

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.

3
  • 1
    Does this support command arguments? i.e. swagger-codegen -i someArg Jun 27, 2018 at 3:09
  • @JacobStamm Yes. Add $* to the end of the doskey command.
    – DavidPostill
    Jun 27, 2018 at 8:20
  • 1
    @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, 2018 at 10:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .