【问题标题】:Error setting alias in PowerShell to Java platform在 PowerShell 中将别名设置为 Java 平台时出错
【发布时间】:2021-05-29 06:19:24
【问题描述】:

我使用的是 Windows 10,但无法使用 PowerShell 为 Java 平台设置别名。

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

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

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 时,它会返回此错误:

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

我还尝试了以下方法,基于How do I set an alias for a specific command in Powershell?,它返回相同的错误: Function mytest { java -Xms1g -Xmx4g -cp "D:\mypath\myfile.jar myfile.myapp" $args }

【问题讨论】:

    标签: powershell windows-10


    【解决方案1】:

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

    所以确实需要一个函数(更多信息请参见this answer)。

    相当于下面的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 来传递传递给函数的参数。

    【讨论】:

      猜你喜欢
      • 2014-12-01
      • 1970-01-01
      • 2022-11-11
      • 1970-01-01
      • 1970-01-01
      • 2021-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多