1

I try to compile a java project with multiple .java files that are in multiple directories. The only flag that I think will help me is the @filename where filename stands for a file with all .java paths.

The PowerShell command that I run is:

javac @sources.txt

And this is the error:

The splatting operator '@' cannot be used to reference variables in an expression. '@sources' can be used only as an argument to a command. To reference variables in an expression use '$sources'.

I mention that I tested this command with cmd and the result is the same.

It is important to compile the java project using powershell, not ant. I am also pleased with a solution that would compile all my .java files without using this option from javac.

What I want to do further is to create a jar file. In order to do this I have to compile the java files.

cd $basedir

cmd /r dir "*.java" /s /B  > sources.txt

javac "@sources.txt"

That is what I do and the sources.txt file is fine.

learner123
  • 165
  • 10
  • 1
    If I execute `cmd /r dir /s /B > sources.txt` then `sources.txt` will contain the full path to itself as the first argument. That would **not** be a correct `javac` argument. I suggest you limit the listing to explicitly just `*.java` files: `cmd /r dir "*.java" /s /B`. – Joachim Sauer Apr 16 '21 at 09:14

1 Answers1

1

That error message implies that PowerShell tries to interpret @sources.txt which you don't want. You want to pass that string verbatim to javac. To do so, you need to quote the argument:

javac "@sources.txt"
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • I already tried that, here it is the error after I run it as string: javac : javac: invalid flag: : At line:1 char:5 + javac "@sources.txt" + ~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (javac: invalid flag: ::String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError – learner123 Apr 16 '21 at 08:46
  • What Java version are you using? What's the output of `javac -version`? – Joachim Sauer Apr 16 '21 at 08:49
  • javac : javac 1.8.0_73 At line:1 char:1 + javac -version + ~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (javac 1.8.0_73:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError – learner123 Apr 16 '21 at 08:53
  • It works for me and I'm don't know PS enough to make sense of it. Please post a [minial, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of your PS script with that demonstrates the error. The `@file` support exists since at least Java 6, so that should not the issue. – Joachim Sauer Apr 16 '21 at 08:55
  • Later edit: Modified the original question – learner123 Apr 16 '21 at 09:00
  • 2
    @learner123: don't use the comments for this kind of info, edit it into your question. – Joachim Sauer Apr 16 '21 at 09:01