简体   繁体   中英

Linux copy files from Java program

I have written a small java code with getRuntime() API to copy files from One directory to another, it it failing, I am not able to understand why? When I run the command from shell it runs fine, can anyone, please let me know the mistake I am doing

    private static void copyFilesLinux(String strSource, String strDestination) {

    String s;
    Process p;
    try {
        // cp -R "/tmp/S1/"*  "/tmp/D1/"
        p = Runtime.getRuntime().exec(
                "cp -R '" + strSource + "/'* '" + strDestination + "/'");
        System.out.println("cp -R \"" + strSource + "/\"* \"" + strDestination + "/\"");
        System.out.println("cp -R '" + strSource + "/'* '" + strDestination + "/'");
        System.out.println(p.toString());
        BufferedReader br = new BufferedReader(new InputStreamReader(
                p.getInputStream()));
        while ((s = br.readLine()) != null)
            System.out.println("line: " + s);
        p.waitFor();
        System.out.println("exit: " + p.exitValue());
        p.destroy();
    }
    catch (InterruptedException iex) {
        iex.printStackTrace();
    }
    catch (IOException iox) {
        iox.printStackTrace();
    }
    catch (Exception e) {
        e.printStackTrace();
    }

}

Output:

cp -R "/tmp/S1/"* "/tmp/D1/"

cp -R '/tmp/S1/'* '/tmp/D1/'

java.lang.UNIXProcess@525483cd

exit: 1

When you use any variation of Runtime.exec() , the binary is called directly , rather than through a shell. That means that wildcards are not supported , because there is no shell to expand them.

I would suggest using Java code to copy your files - it would be far more portable and much safer. Barring that, you can use a shell binary to execute your command via its -c option.

You can do that using standard java api unless you have really a need to execute system commands.

http://docs.oracle.com/javase/tutorial/essential/io/copy.html

It works with the below code,

            String[] b = new String[] {"bash", "-c", "cp -R \"" + strSource + "/\"* \"" + strDestination + "/\""};  
        p = Runtime.getRuntime().exec(b);

I googled it and found the link

http://www.coderanch.com/t/423573/java/java/Passing-wilcard-Runtime-exec-command

It worked for me with following code.

 public static void main(String []args) throws Exception{
        String s;
        Process p;
        try {
            String b[] = new String[4];
            b[0] = "cp";
            b[1] = "-R";
            b[2] = "HelloWorld.java";
            b[3] = "abc.java";

            p = Runtime.getRuntime().exec(b);
            BufferedReader br = new BufferedReader(
                new InputStreamReader(p.getInputStream()));
            while ((s = br.readLine()) != null)
                System.out.println("line: " + s);
            p.waitFor();
            System.out.println ("exit: " + p.exitValue());
            p.destroy();
        } catch (Exception e) {}        
     }
}

Create a String[] of commands and pass the commands in that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM