1

I am using Windows 10 and having trouble setting an alias to a Java platform using PowerShell.

This works in Git Bash: alias mytest='java -Xms1g -Xmx4g -cp D:/mypath/myfile.jar myfile.myapp'

And when I type mytest into the Git Bash prompt, it returns:

myfile [v11.3.0]

Usage:
  java ...

And I can run it without issue in the Git Bash prompt using the alias.

I am trying to learn how to do this in PowerShell also, and here are my attempts and errors:

Set-Alias -Name "mytest" -Value "java -Xms1g -Xmx4g -cp D:\mypath\myfile.jar myfile.myapp" -Description "An alias to for mytest"

And when I enter mytest in the PowerShell prompt, it returns this error:

mytest : The term 'D:\mypath\myfile.jar myfile.myapp' 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
+ mytest
+ ~~~~~~
    + CategoryInfo          : ObjectNotFound: (D:\mypath\myfile.jar myfile.myapp:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

I also tried the following, based on How do I set an alias for a specific command in Powershell?, and it returns the same error: Function mytest { java -Xms1g -Xmx4g -cp "D:\mypath\myfile.jar myfile.myapp" $args }

a11
  • 3,122
  • 4
  • 27
  • 66

1 Answers1

2

Aliases in PowerShell are simply alternative names for other commands, which precludes defining aliases with (hard-coded) arguments, the way that POSIX-compatible shells such as bash allow.

So a function is indeed necessary (see this answer for more information).

The equivalent of the following bash alias:

# bash
alias mytest='java -Xms1g -Xmx4g -cp D:/mypath/myfile.jar myfile.myapp'

is this PowerShell function:

# PowerShell
function mytest { java -Xms1g -Xmx4g -cp D:/mypath/myfile.jar myfile.myapp $args }

Note how the automatic $args variable is needed to pass arguments passed to the function through.

mklement0
  • 382,024
  • 64
  • 607
  • 775