javac powershell classpath separator

2.2k views Asked by At

So I'm aware that different operating systems require different classpath separators. I'm running a build of windows where CMD has been replaced with Powershell which is causing problems when using the semi-colon separator.

The command I'm trying to run begins with cmd /c to try and get it to run in command prompt instead but I think when PowerShell is parsing the whole command it sees the semi-colon and thinks that is the end!

My whole command is:

cmd /c javac -cp PATH1;PATH2 -d DESTINATION_PATH SOURCE_PATH

I have tried using a space, colon and period to no avail. Can anybody suggest a solution?

This is my first question on stackoverflow, hope the community can help and that it will eventually help others. :)

3

There are 3 answers

3
YanivK On BEST ANSWER

I suggest you start the process in the following way using Powershell

Start-Process cmd.exe -ArgumentList "/c javac -cp PATH1;PATH2 -d DESTINATION_PATH SOURCE_PATH" -NoNewWindow
2
Ansgar Wiechers On

Running javac in CMD shouldn't be required. Just put quotes around arguments that (may) contain whitespace or special characters. I'd also recommend using the call operator (&). It's optional in this case, but required if you put the executable in quotes (e.g. because the executable or path contains spaces or you want to put it in a variable).

& javac -cp "PATH1;PATH2" -d "DESTINATION_PATH" "SOURCE_PATH"

You could also use splatting for providing the arguments:

$javac  = "$env:JAVA_HOME\bin\javac.exe"
$params = '-cp', "PATH1;PATH2",
          '-d', "DESTINATION_PATH",
          "SOURCE_PATH"

& $javac @params
1
AKumar On

javac -classpath "path1:path2:." main.java does the trick in powershell. the cmd doesn't need the doble quotes however while using powershell we need to put the quotes and it works smoothly.