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 }

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.