I have read this documentation including the section that exemplifies Command Line Argument Files but can't figure out how to get it working in PowerShell. Here's what I have done:
First, I can compile a Driver.java file using (in windows PowerShell, javac version 17.0.3):
>>javac Driver.java
But, I would like to figure out how to use argument files (@filename
).
I have tried to create a file called files.txt
with 1 entry in it listed as Driver.java
and then use javac with @file to specify what to compile:
PS-pluto>>dir -n *.java >files
PS-pluto>javac @files
I tried creating files
, and files.txt
. Driver.java is definitely the entry, but the javac command doesn't work. I am running this from the folder where files and Driver are located.
I read the answer to this question which indicated that I needed to add quotes, and tried this (again, with and without .txt):
PS-pluto>>dir -n *.java >files
PS-pluto>>javac "@files"
This may work, but doesn't solve the problem. Now I get a new error:
error: invalid flag: ■Driver.java
I have looked for hidden formatting in my txt file and only see the line return at the end of Driver.java. Not sure what is wrong?
I know in this simplistic case using an argument file is overkill, but I am simply trying to figure out how it works...and cannot.
NOTE: I already know how to compile this in an IDE or with a build tool. I am trying to figure out how to do this in PowerShell, and admittedly, I am a novice PowerShell user.
CodePudding user response:
An unquoted
@
at the start of an argument is a PowerShell metacharacter and therefore needs to either be escaped as`@
or enclosed in a string literal.In Windows PowerShell,
>
creates "Unicode" (UTF-16LE) files by default, whichjavac
most likely cannot handle.In PowerShell (Core) 7 , BOM-less UTF8 is now the (consistent) default, so
>
would work as is.In Windows PowerShell, the simplest solution is to pipe to
Set-Content -Encoding Ascii
, assuming that the file names contain ASCII-range characters only.
# Create file "files" as an ASCII file.
# Note: The Get-ChildItem call is `dir -n *.java` fully spelled out.
Get-ChildItem -Name *.java | Set-Content -Encoding Ascii files
# Escape @ as `@ to treat it literally.
javac `@files
If the file names contain non-ASCII characters and javac
requires BOM-less UTF-8 encoding, more work is needed, because in Windows PowerShell you cannot directly create BOM-less UTF-8 files - see this answer.