leahsaif leahsaif - 1 month ago 12
Java Question

Mapped Network Drive using Java is Not Connected

I am trying to map a network drive using the command 'net use' via Java.

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.

UAT PC - Windows Server 2008 R2 Standard Service Pack 1.
My PC - Windows 7 Pro Service Pack 1

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.

Below are the codes I have tried so far but produces the same result.

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 && 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() > 0)
{
Log.info(messageLine);
}
}
}
else if (exitStatus == 2)
{
Log.info("Connection already established!");
}
else
{
Log.info("Connection successful!");
}
}
catch (Exception e)
{
e.printStackTrace();
}
}


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.

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?

Answer Source

Posting an answer for future reference. When I execute the net use command without a letter it works. It does not create a drive letter but i am able to access it if I type the full path (ipaddress\folder). The command string I used is

net use " + "\\\\ipaddress\\folder /user:" + USER_NAME + " " + PASSWORD

... did the trick! My ID has full access permission by the way.

You just have to check though if the folder exists if (!myDirectory.exists()) to know if you're connected or not.