0

I want to copy some files from my computer to a server-location.

My computer is running on Windows-7. My files are stored in :

C:\Transfer\

The server location where I have to transfer the files is :

\\server1\myname\TransferData\

I want to do this using Java. I have tried some commands like this on my command-prompt :

pushd \\server1\myname\TransferData\
Z:\> mv C:\Transfer\* Z:\

For some reason, this works when done manually & DOES NOT work through java. I get IOException.

Java Code I have been using :

Process proc = Runtime.getRuntime().exec("pushd \\server1\myname\TransferData\");
proc.waitFor();
// once this server location gets mounted - i was thinking of moving the file. that part works through java.

Error I get is this :

Exception in thread "main" java.io.IOException: Cannot run program "pushd": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at com.data.Main.main(Main.java:34)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)

I have also tried this :

Process proc = Runtime.getRuntime().exec("cmd pushd \\server1\myname\TransferData\");
proc.waitFor();

&

Process proc = Runtime.getRuntime().exec("cmd \c pushd \\server1\myname\TransferData\");
proc.waitFor();

&

Process proc = Runtime.getRuntime().exec("cmd.exe pushd \\server1\myname\TransferData\");
proc.waitFor();

The above does not throw an exception. And does not mount the server location to my computer also.

I have write access in the server. I really need a solution to my problem. Thanks.

sudhishkr
  • 3,318
  • 5
  • 33
  • 55
  • What's the Java code you are using? What are the details of the IOException? – Ted Hopp Jan 09 '15 at 05:28
  • Have you tried running pushd inside cmd environment. For eg: Runtime.getRuntime().exec("cmd \c ...") – Tarun Jan 09 '15 at 05:51
  • I tried that once, and I get NO Exceptions. But again - nothing happens. I will append this to my question now. If you can take a look at it and let me know if its right - that will help. – sudhishkr Jan 09 '15 at 05:54
  • When a UNC path is specified, `PUSHD` will create a temporary drive map and will then use that new drive. But how-long temporary this _temporary drive map_ stands for? Will it last when calling next `.exec` in Java? I'd try `.exec("cmd /E:ON /C pushd \\server1\myname\TransferData\&&mv C:\Transfer\* Z:\")` (supposing that `mv` works, I don't know it...). Or create a stable mapping with `net use ...` And if Command Extensions are disabled the PUSHD command will not accept a network (UNC) path, therefore `/E:ON` switch – JosefZ Jan 10 '15 at 13:21

1 Answers1

1

Why not use JSCH? I've been successfully transferring files from a Windows computer to a Linux machine. In fact I was searching for the reverse when I came here. You can use the program below:

            import com.jcraft.jsch.Channel;
            import com.jcraft.jsch.ChannelSftp;
            import com.jcraft.jsch.JSch;
            import com.jcraft.jsch.JSchException;
            import com.jcraft.jsch.Session;
            import com.jcraft.jsch.SftpException;

            public class FileTransfer {
                public static void main(String args[]) {
                    String hostname = "";
                    String username = "";
                    String password = "";
                    String copyFrom = "";
                    String copyTo = ""; 
                    JSch jsch = new JSch();
                    Session session = null;
                    System.out.println("Trying to connect.....");
                    try {
                        session = jsch.getSession(username, hostname, 22);
                        session.setConfig("StrictHostKeyChecking", "no");
                        session.setPassword(password);
                        session.connect(); 
                        Channel channel = session.openChannel("sftp");
                        channel.connect();
                        ChannelSftp sftpChannel = (ChannelSftp) channel; 
                        sftpChannel.get(copyFrom, copyTo);

                        sftpChannel.exit();
                        session.disconnect();
                        System.out.println("Done !!");
                    } catch (JSchException e) {
                        e.printStackTrace();  
                    } catch (SftpException e) {
                        e.printStackTrace();
                    }

                }
            }
nEO
  • 5,305
  • 3
  • 21
  • 25