Solved

Strange behaviour of scp

Posted on 2004-08-02
15
542 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
oracle 11g 23 107
Java string replace 11 55
ejb on wildfly 5 30
wild fly 8 startup error 2 14
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

820 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