Solved

Strange behaviour of scp

Posted on 2004-08-02
15
541 Views
Last Modified: 2012-05-05
Hi!

I have some strange behaviour of the scp.exe (Cygwin on Win2k)
When I start the Programm manually on the Commandline it gives me some fine output (percentage, eta, speed ...)
But when I try to start the same thing in a java-process (via Runtime.exec())
neither the output nor the error stream give me any data.

scp itself works fine, meaning the file is successfully transfered, and the streams are closed afterwards.
But where is my output?

Anyone a clue?

Bye,
marko
0
Comment
Question by:karlika
  • 6
  • 5
  • 2
  • +1
15 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 125 total points
ID: 11693471
You probably are not handling stderr and stdin asynchronously. Try this:

SNIP========================================================================


public class CommandRunner {

      private int returnValue = Integer.MIN_VALUE;



      public int getReturnValue() {
            return returnValue;
      }



      public static void main(String[] args) {
            new CommandRunner().run(args);
      }






      public void run(String[] args) {

            try {

                  if (args.length < 1) {
                        System.out.println("Usage: java CommandRunner <command string>");
                        System.exit(-1);
                  }
                  Process pro = null;
                  if (args.length > 1) {
                        pro = Runtime.getRuntime().exec(args);
                  }
                  else {
                        pro = Runtime.getRuntime().exec(args[0]);
                  }
                  InputStream error = pro.getErrorStream();
                  InputStream output = pro.getInputStream();
                  Thread err = new Thread(new OutErrReader(error));
                  Thread out = new Thread(new OutErrReader(output));
                  out.start();
                  err.start();
                  returnValue = pro.waitFor();
            }
            catch (java.io.IOException e) {
                  e.printStackTrace();
            }
            catch (java.lang.InterruptedException e) {
                  e.printStackTrace();
            }

      }


      class OutErrReader implements Runnable {
            InputStream is;


            public OutErrReader(InputStream is) {
                  this.is = is;
            }


            public void run() {
                  try {
                        BufferedReader in = new BufferedReader(new InputStreamReader(is));
                        String temp = null;
                        while ((temp = in.readLine()) != null) {
                              System.out.println(temp);
                        }
                        is.close();
                  }
                  catch (Exception e) {
                        e.printStackTrace();
                  }
            }
      }
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11693493
Use as follows:

CommandRunner.main(new String[] { "scp.exe", "arg1", "arg2" });
0
 
LVL 2

Author Comment

by:karlika
ID: 11693541
Well, Im allready doing this.
It worked fine until I switched to a newer Version of cygwin.
The only thing I get now from output.readLine() and error.readLine() is null.

Regards,
Marko
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 86

Expert Comment

by:CEHJ
ID: 11693573
I'm not familiar with scp.exe. You don't have to run an additional shell interpreter do you? i.e. something like

scp.exe sh <some shell script>

?
0
 
LVL 2

Author Comment

by:karlika
ID: 11693604
Oke, it does not seem to be a Java Problem, since the command:
scp [some options] > testout 2>&1

Which should print every output into the testout file delivers an empty file.
But, how does the output come to the screen if not over the standard output and error -streams?

And how can I access this output?

Regards,
Marko
0
 
LVL 2

Author Comment

by:karlika
ID: 11693649
No, you just call the programm directly.
No additional interpreters should be involved...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11693834
Does the simpler

scp [some options] > testout

work?
0
 
LVL 35

Accepted Solution

by:
TimYates earned 125 total points
ID: 11693868
Yeah, I don't believe you can redirect the output scp creates (as it is an interactive progress meter)...

What you can do, is use the exit code to work out if the operation completed successfully :-)

It returns 0 if it was successful, or something else if it failed...

http://sunportal.sunmanagers.org/pipermail/summaries/2002-May/001710.html

Tim
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11693892
So basically, check that

    returnValue = pro.waitFor();

(or whatever you have) returns 0
0
 
LVL 2

Author Comment

by:karlika
ID: 11695610
BAAAD scp.
Im using the output to draw a download progress bar, so the exit code does not really help me.

Anyone know another windows scp implementation which works with SSH 2?
I used pscp before but it doesnt support ssh2 identity files, which I am bound to use.

Regards,
marko
0
 
LVL 92

Expert Comment

by:objects
ID: 11699977
> But, how does the output come to the screen if not over the standard output and error -streams?

Its probably using a different stream than stdout or stderr.
0
 
LVL 2

Author Comment

by:karlika
ID: 11702467
Im just checking the filesize periodically to power my progress bar...

Thanks for your comments
0
 
LVL 92

Expert Comment

by:objects
ID: 11702502
heres another example of redirecting process output. It sends it to a text area but modifying to update a progress bar would be a simple change.

http://www.objects.com.au/java/examples/util/SwingExec.do

You just need to work out where scp is sending its output.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11702576
>> But, how does the output come to the screen if not over the standard output and error -streams?

It will use stderr/stdout. You will probably have to ditch the stream redirection in the command itself,handling it instead with Java
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11702583
:-)
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question