You are right, java -version output to stderr.

On May 5, 2010, at 3:27 AM, Heiner Westphal wrote:

> many programs on unices use stdout for data and stderr for error and 
> informational messages.
> This way you can chain commands by piping stdout and still see warnings
> which go to stderr and would otherwise get confused with incoming data.
> 
> e.g.:
> $ grep ^/dev/ /etc/mtab /etc/ntab | wc -l
> grep: /etc/ntab: No such file or directory
> 7
> 
> 
> wc got the data, but I still can see if s.t. went wrong
> with grep.
> 
> If version information is data or other information is just
> a matter of taste.
> Since java does not support the -version option while running
> a class, I would have expected stdout as you did, because
> there is no other data to be expected.
> 
> Regards,
> 
> Heiner
> 
> Haidong Zhu wrote:
>> Thanks for reply, but why the message i need comes from err stream?
>> On May 4, 2010, at 11:40 AM, Keith Alan Richardson wrote:
>>> You print only the stdout or stderr, depending on the exit code
>>> 
>>> Try printing both.
>>> 
>>> 
>>>> On May 4, 2010 6:27 AM, "Haidong Zhu" <[email protected] 
>>>> <mailto:[email protected]>> wrote:
>>>> 
>>>>   Below is my code,if i set the command as "ls ~/" I can get the output of 
>>>> command ls by reading the input stream from channel.
>>>> 
>>>>   if i set the command as "java -version", i can not get any output and 
>>>> the exitcode is 0.
>>>> 
>>>>   command = "ls ~/";
>>>>   //command = "java -version";
>>>>   InputStream in = null;
>>>>   InputStream err = null;
>>>>   BufferedReader inReader = null;
>>>>   BufferedReader errReader = null;
>>>>   try {
>>>>     JSch jsch = new JSch();
>>>>     Session session = jsch.getSession(user, host, port);
>>>>     session.setUserInfo(userInfo);
>>>>     session.setTimeout(timeout);
>>>>     session.connect();
>>>>     Channel channel = session.openChannel("exec");
>>>>     ((ChannelExec)channel).setCommand(command);
>>>>     channel.setInputStream(null);
>>>>     ((ChannelExec)channel).setErrStream(null);
>>>> 
>>>>     in = channel.getInputStream();
>>>>     err = ((ChannelExec)channel).getErrStream();
>>>> 
>>>>     channel.connect();
>>>> 
>>>>     int exitCode = 0;
>>>>     while(true) {
>>>>       if(channel.isClosed()){
>>>>         exitCode = channel.getExitStatus();
>>>>         break;
>>>>       }
>>>>       try {
>>>>         Thread.sleep(1000);
>>>>       } catch (InterruptedException ie) {
>>>>       }
>>>>     }
>>>> 
>>>>     inReader = new BufferedReader(new InputStreamReader(in));
>>>>     errReader = new BufferedReader(new InputStreamReader(err));
>>>> 
>>>>   if (exitCode != 0) {
>>>>     while ((s = errReader.readLine()) != null) {
>>>>       sb.append(s).append("\n");
>>>>     }
>>>>     status.setData(sb.toString());
>>>>   } else {
>>>>     while ((s = inReader.readLine()) != null) {
>>>>       sb.append(s).append("\n");
>>>>     }
>>>>     System.out.println(sb.toString());
>>>>   }
>>>> 
>>>> 


------------------------------------------------------------------------------
_______________________________________________
JSch-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jsch-users

Reply via email to