How to make alias for jar command

Hello everybody,

today I want to write few words about aliases in Powershell. First of all, why somebody may need aliases? Need in aliases arises, when you have some kind of big cumbersome commands. For example recently I've got a need to execute something like this:

java -jar f:\utils\ANTLR\tpantlr2-code\code\antlr-4.7.1-complete.jar

As you can see from the length, to type such a long string is pretty hard, even if you copy/paste it. In that case I've decided to use aliases. 

My initial approach was like this:

Set-Alias antlr4 'java -jar f:\utils\ANTLR\tpantlr2-code\code\antlr-4.7.1-complete.jar

But when I've tried to execute such alias, Ive got the following error message:

antlr4 : The term 'java -jar f:\utils\ANTLR\tpantlr2-code\code\antlr-4.7.1-complete.jar' is not recognized as the
name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was
included, verify that the path is correct and try again.
At line:1 char:1
+ antlr4
+ ~~~~~~
+ CategoryInfo : ObjectNotFound: (java -jar d:\so....1-complete.jar:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

Disappointing? Yeah. After googling a bit I have found that solution for such a problem lies in making function, and then set function to alias. Here is how I've achieved it:

function RunAntlr4 {java -jar f:\utils\ANTLR\tpantlr2-code\code\antlr-4.7.1-complete.jar}

Set-Alias antlr4 RunAntlr4

After those two lines, alias worked perfectly fine.

No Comments

Add a Comment