1

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. :)

1
  • 2
    -cp "PATH1;PATH2" Commented Jan 7, 2017 at 18:59

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

3 Comments

What do you mean by "CMD has been replaced" ? CMD is available in all Microsoft operating systems, including but not limited to the latest "Windows 10".
The machine it does not work on is using a technical preview of Windows from the Windows Insider program. I'm referring to link So CMD is around still but I believe under the hood from my java program that builds the command String[] PowerShell is being used to execute it. And my results from copy-pasting the command into CMD and PowerShell support that.
Thank you for the solution. To elaborate on PowerShell 'replacing' CMD. I was thinking that with the latest preview builds of windows the process created by java code Process pro = Runtime.getRuntime().exec(String[] cmdArray, String[] envp, File dir); was run in PowerShell not Command Prompt now. Although my initial attempts seemed to support that theory, the error was actually in a different place.
0

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

2 Comments

Thank you for the comment regarding putting PATH1;PATH2 in double quotes. That solved the problem in part. I can call javac from CMD but I don't think I have set up properly for PowerShell to work with just javac. I'm pretty sure your splatting solution would do the trick though.
The only thing you should need for a basic setup are the environment variable JAVA_HOME (pointing to the folder where your JDK is installed) and the path to javac.exe (usually %JAVA_HOME%\bin) in your PATH environment variable. Both environment variables affect CMD and PowerShell, i.e. if you have your setup working in CMD it should also be working in PowerShell.
0

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.

1 Comment

Although this answer is on the right track, the ':' will not work. It needs to be a semicolon ';'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.