I have written a basic powershell script like this:
$ "java.exe" "-cp" "." $args
My test Java program:
public static void main(String[] args)
{
for(String arg : args) System.out.println(arg);
}
I use this script to run a Java program like this:
.\test.ps1 com.test.Test *.txt
I expect/want for this program to print this:
*.txt
Instead it prints out every file name that matches the file card in that directory like this:
1.txt
2.txt
3.txt
Does anyone have any ideas how I can prevent the wildcard expansion in the powershell script? I have tried putting double and single quotes around the &args and that doesn't work. I tried splitting all the arguments into another array and that didn't work.
Update 1:
.\test.ps1 "*.txt"
Results in the same results:
1.txt
2.txt
3.txt
Update 2:
.\test.ps1 \*.txt
Results in an incorrect argument printed:
\*.txt
Solution
I finally figured this out. You need to re-populate and quote the arguments in the powershell script like this:
$QuotedArguments = @() #Creating an array of arguments
foreach($arg in $args)
{
#This is done because arguments with * may be expanded
#into matching file names instead of left alone
$QuotedArguments += """$arg"""
}
$ "java.exe" "-cp" "." $QuotedArguments