Solved

Strange behaviour of scp

Posted on 2004-08-02
15
545 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses

623 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