1

I'd like to command javac & java at the same time but it's not work and it return below exception :

At line:1 char:17
+ javac Demo.java && java Demo
+                 ~~
The token '&&' is not a valid statement separator in this version.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : InvalidEndOfLine

image

if i run separately it's work

PS D:\git\ITWeiHanDEV\Java> javac Demo.java

PS D:\git\ITWeiHanDEV\Java> java Demo   

image

ps :

  • my version is java8
  • it's windows 10 visual studio code terminal
Wei Lin
  • 3,591
  • 2
  • 20
  • 52

2 Answers2

2

I tried to change vscode terminal type to cmd not powershell integrated console and run below script and it's work

cd "d:\git\ITWeiHanDEV\Java\" && javac Demo.java && java Demo

image

Wei Lin
  • 3,591
  • 2
  • 20
  • 52
  • 2
    Yes, `cmd.exe`, the Windows legacy shell, has always supported `&&` and `||`, whereas PowerShell started supporting these operators only from v7.0 – mklement0 Aug 07 '20 at 02:11
2

PowerShell versions 6.x and earlier, including Windows PowerShell, do not support && and ||, the pipeline chain operators - they're only available in PowerShell [Core] 7+

In 6.x and earlier, you can emulate the behavior of && as follows:

javac Demo.java; if ($LASTEXITCODE -eq 0) { java Demo }
mklement0
  • 382,024
  • 64
  • 607
  • 775