Search code examples
java-8processprocessbuilder

Java 8 Acces Denied while running process with arguments


In my program i need to run exe file in process. I'm doing it with ProcessBuilder. When i'm putting to code only directory and exe name, process is running normally, but i want to put arguments. When i'm trying it i'm getting exception with Acces Denied message. It's my code:

Process process = new ProcessBuilder("C:\\Directory", "file.exe", argument1).start();

What is wrong with it?

My earlier code, that worked but without arguments was:

 String folder = "C:\\Directory";
 String exe = "File.exe";  
 ProcessBuilder pb = new ProcessBuilder();
 pb.command(folder + exe);
 pb.start();

With this code i was able to see started process in ProcessManager.


Solution

  • Your code is trying to execute C:\\Directory which is not allowed.

    The full path of the executable must be in the first argument to the constructor, so:

    Process process = new ProcessBuilder("C:\\Directory\\file.exe", argument1).start();
    

    This is assuming C:\Directory\file.exe is the program you are trying to run.

    Update: In your original code you have:

     String folder = "C:\\Directory";
     String exe = "File.exe";  
    

    so 'folder + exe' is C:\DirectoryFile.exe so you the equivalent code is:

    Process process = new ProcessBuilder("C:\\DirectoryFile.exe", argument1).start();