在 PowerShell 中将别名设置为 Java 平台时出错

Error setting alias in PowerShell to Java platform

我正在使用 Windows 10,但在使用 PowerShell 为 Java 平台设置别名时遇到问题。

这适用于 Git Bash: alias mytest='java -Xms1g -Xmx4g -cp D:/mypath/myfile.jar myfile.myapp'

当我在 Git Bash 提示符中输入 mytest 时,它 returns:

myfile [v11.3.0]

Usage:
  java ...

而且我可以 运行 使用别名在 Git Bash 提示符中毫无问题。

我也在尝试学习如何在 PowerShell 中执行此操作,以下是我的尝试和错误:

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

当我在 PowerShell 提示符中输入 mytest 时,它 returns 这个错误:

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

我也尝试了以下,基于,它returns同样的错误: Function mytest { java -Xms1g -Xmx4g -cp "D:\mypath\myfile.jar myfile.myapp" $args }

PowerShell 中的别名只是替代 names 用于其他 commands,这排除了定义别名 使用(硬编码)参数 ,POSIX 兼容的 shell,例如 bash 允许的方式。

所以函数确实是必要的(更多信息参见)。

等同于以下 bash 别名:

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

这是 PowerShell 函数吗:

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

请注意如何需要 automatic $args variable 来传递传递给函数的参数。