powershell - Error setting alias in PowerShell to Java platform
问题描述
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 }
解决方案
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.
推荐阅读
- javascript - 可以将条件语句作为函数参数传递吗?
- scada - TeslaScada 2(等待)中的睡眠当前线程?
- wordpress - 如何在 WooCommerce 中更改添加到购物车通知的样式?
- python - Errno 2] 没有这样的文件或目录:'$(Agent.ToolsDirectory)/azcopy/azcopy'
- sql - SQL合并查询中需要过滤目标表
- r - 使用 R 中的 plotly 在图表中添加按钮进行注释
- node.js - pg 节点 + POSTGRES 列名不存在
- python - Yfinance 未在 1 分钟内给出加密货币的准确价格
- ios - 如何从 Optional(Optional(<__NSSingleObjectArrayI >(25))) 中获取值
- c# - EF Core:无法翻译查询 - 但在 LinqPad 中工作正常