Souvik Saha
Souvik Saha

Reputation: 1

Need to share a Windows folder in Remote Server using Java

I am trying to share a Windows folder in remote server using my Java Code. To achieve this, I am using a process and net share command to accomplish this but getting syntax error. I have searched a lot but could not get a proper syntax on how to share in remote server. If you have any idea suggest.

try {
    String commandString = "net share sharefolder=\\USMLVV3BI0567\D$\Data /grant:everyone,FULL /UNLIMITED /REMARK:Share /CACHE:None /grant:everyone,FULL /UNLIMITED /REMARK:Share /CACHE:None";
    Process p;
    System.out.println(commandString);

    p = Runtime.getRuntime().exec(commandString);
    // To get the output of command
    BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

    BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
    String line = null;
    while (true) {
        line = stdInput.readLine();
        if (line == null) {
            break;
        }
        System.out.println(line);
    }
    while (true) {
        line = stdError.readLine();
        if (line == null) {
            break;
        }
        System.out.println(line);
    }
}
catch (IOException e) {
    e.printStackTrace();
}

Upvotes: 0

Views: 82

Answers (1)

Abra
Abra

Reputation: 20913

but getting syntax error

Do you mean...

Invalid escape sequence (valid ones are  \b  \t  \n  \f  \r  \"  \'  \\ )

That's because you need to escape the backslash, i.e. \, characters in the String.

Try the following:

String commandString = "net share sharefolder=\\\\USMLVV3BI0567\\D$\\Data /grant:everyone,FULL /UNLIMITED /REMARK:Share /CACHE:None /grant:everyone,FULL /UNLIMITED /REMARK:Share /CACHE:None";

Upvotes: 0

Related Questions