title: Mapped Network Drive using Java is Not Connected
body: <p>I am trying to map a network drive using the command 'net use' via Java.</p>
  
  <p>I have tried several approaches that I have seen on the net but none seem to work on our UAT PC but always works on my PC.</p>
  
  <p>UAT PC - Windows Server 2008 R2 Standard Service Pack 1.
  My PC - Windows 7 Pro Service Pack 1 </p>
  
  <p>What happens is the method creates a new drive but the drive icon has an X mark indicating that something is wrong and when I click it, it is saying:
  "Location is unavailable" [Message Title]
  Can not access L: .
  Logon failure: unknown user name or password incorrect.</p>
  
  <p>Below are the codes I have tried so far but produces the same result.</p>
  
  <pre><code>public void connectToRemoteServer()
  {
    try
    {
      String USER_NAME = "domain\\username";
      String PASSWORD = "password";
      /** ATTEMPT 1 - Call command from JAVA using string - OK in LOCAL, FAILED in UAT */
      // String commandInside = "net use " + REMOTE_FILE_SERVER_DRIVE + ": \\\\ipaddress\\folder /user:" + USER_NAME + " " + PASSWORD;
      // Process p = Runtime.getRuntime().exec(commandInside);
      // Log.info("Executing command [" + commandInside + "]");
      // p.waitFor();
  
      /** ATTEMPT 2 - Call command from JAVA using an array of string - OK in LOCAL, FAILED in UAT */
      // String[] commandInside =  {"c:\\windows\\system32\\net.exe", "use", REMOTE_FILE_SERVER_DRIVE + ":", "\\\\ipaddress\\folder", "/user:" + USER_NAME, PASSWORD };
      // Process p = Runtime.getRuntime().exec(commandInside);
      // Log.info("Executing command [" + commandInside + "]");
      // p.waitFor();
  
      /** ATTEMPT 3 - Call command from JAVA using a process builder  - OK in LOCAL, FAILED in UAT */
      // String[] commandInside =  { "cmd.exe", "/C", "net", "use", REMOTE_FILE_SERVER_DRIVE + ":", "\\\\ipaddress\\folder", "/user:" + USER_NAME, PASSWORD };
      // Log.info("Constructed command [" + Arrays.toString(commandInside) + "]");
      // ProcessBuilder pb = new ProcessBuilder(commandInside);
      // Log.info("Starting command");
      // Process process = pb.start();
      // Log.info("Waiting for command to complete");
      // process.waitFor();
  
      /** ATTEMPT 4 - Write the command in a cmd/bat file then execute that cmd/bat file from JAVA using an array of string  - OK in LOCAL, FAILED in UAT*/
      File batchFile = new File(ROOT_PATH + "MapRemoteServer.cmd");
  
      String commandInside = "net use " + REMOTE_FILE_SERVER_DRIVE + ": \\\\ipaddress\\folder/user:" + USER_NAME + " " + PASSWORD;
      batchFile.createNewFile();
      FileWriter fw = new FileWriter(batchFile);
      fw.write(commandInside + "\r\n\r\nexit");
      fw.close();
  
      Thread.sleep(500);  // just to ensure that the file is properly closed before running it
      String[] command = { "cmd.exe", "/c", "Start", ROOT_PATH + "MapRemoteServer.cmd" };
  
      Process process = Runtime.getRuntime().exec(command);
      process.waitFor();
  
  
      int exitStatus = process.exitValue();
      Log.info("Exit status: [" + exitStatus + "]");
  
      if (exitStatus != 0 &amp;&amp; exitStatus != 2)
      {
        BufferedReader stdError = new BufferedReader(new  InputStreamReader(process.getErrorStream()));
        String messageLine = null;
        Log.info("Error/Warning encountered during execution of net use!");
        while ((messageLine = stdError.readLine()) != null) {
          if (messageLine.length() &gt; 0) 
          {
            Log.info(messageLine);
          }
        }
      }
      else if (exitStatus == 2)
      {
        Log.info("Connection already established!");
      }
      else
      {
        Log.info("Connection successful!");
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }
  </code></pre>
  
  <p>If I run the actual command in command line (from Attempt 1) or I execute the batch file (from Attempt 4) itself, it works. Only when it is executed by my Java code that it doesn't.</p>
  
  <p>Aside from net use, is there another way to map a drive in windows via Java?
  Is there some sort of setup that can be done in a Windows PC that might prevent mapping from a program?</p>