Search code examples
javalinuxruntime.execprocessbuildersamba

Running smbclient command from Java using Runtime


I am trying to run smbclient command to copy a file to a share drive using Java Runtime class with the following code:

private void copyFiles(String filePath) throws Exception {

    String command = "smbclient -A smbclient_authentication.txt //192.14.34.118/testbakup -c \"put " + filePath + "\"";
    System.out.println("Smbclinet command:" + command);
    Process p = Runtime.getRuntime().exec(command);
    int waitFor = p.waitFor();

    if (waitFor == 0) {
        System.out.println(p.exitValue());
        StringBuffer sb = new StringBuffer();
        BufferedReader reader =
                new BufferedReader(new InputStreamReader(p.getInputStream()));

        String line = "";
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        if (reader != null) reader.close();
    } else {

        InputStream errorStream = p.getErrorStream();
        byte[] buffer = new byte[errorStream.available()];
        errorStream.read(buffer);

        String str = new String(buffer);
        System.out.println(str);
        if (errorStream != null) errorStream.close();
    }

}

I tried using JCIFS library but it is taking too much time to copy a file. so i want to run the above command using Java. I am able to run the same command from outside but not from Java and it is not even giving any error as well.


Solution

  • I mounted that drive into my machine and copied the file to that mounted directory.