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.

4
  • What's the Java code you are using? What are the details of the IOException? Commented Jan 9, 2015 at 5:28
  • Have you tried running pushd inside cmd environment. For eg: Runtime.getRuntime().exec("cmd \c ...") Commented Jan 9, 2015 at 5: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. Commented Jan 9, 2015 at 5: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 Commented Jan 10, 2015 at 13:21

1 Answer 1

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();
                    }

                }
            }
Sign up to request clarification or add additional context in comments.

1 Comment

i had tried something like this and got it working. this is good - thanks for the help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.