Solved

Strange behaviour of scp

Posted on 2004-08-02
15
543 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Certificat to iSeries KeyStore 1 30
CSV file parsing thru Java 13 54
How to fix  socket closed error 11 64
Selenium WebElement Login button findElement 9 20
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 …
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:
Suggested Courses

738 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