0

Environment: Windows 10 (Java) -> Windows 10 (PowerShell and C#) Java: 1.8.0_252

Trying to remotely execute a C# program via PowerShell from Java. Seem to be having issues with the path. The C# program is in a subdirectory under a shared drive.

import java.io.*;
public class CallCSharp {
    public static void main(String[] args) {
        try {
            ProcessBuilder builder = new ProcessBuilder("powershell.exe", "/c",
             "\\SharedDrive\\Data\\Bin\\Program.exe \\\\10.1.1.1 -u user -p password");
            builder.redirectErrorStream(true);
            Process p = builder.start();
            BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line;
            while (true) {
               line = r.readLine();
               if (line == null) {
                    break;
               }
                System.out.println(line);
            }
        } catch (Exception e){
           e.printStackTrace();
        }
    }
}

The error returned is

The term '\SharedDrive\Data\Bin\Program.exe' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

The path is correct and the C# program can be run successfully. Suspect I am not formatting the ProcessBuilder call correctly.

9
  • 1
    Can you try with absolute path to the exe. Commented Aug 5, 2020 at 15:58
  • @YannCha - please provide an example. Commented Aug 5, 2020 at 15:59
  • 1
    similar command in this answer : stackoverflow.com/questions/35543828/… Commented Aug 5, 2020 at 16:18
  • 1
    So, it shouldn't be \\\\ShareDrive then? Commented Aug 5, 2020 at 16:38
  • 1
    I'd guess, I'm no Java coder but it just didn't make sense the way you escaped `` in every other occurence other than with the network share. Commented Aug 5, 2020 at 16:42

0

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.