Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Capturing output from running external program

Status
Not open for further replies.

jamescpp

IS-IT--Management
Aug 29, 2001
70
US
I posted this several days to Sun's forum but cannot seem to get an answer. Can anyone here give me some help? Thanks.

I'm using exec() to run an ftp session with a script. I'm trying to capture the output of the script. I've found many previous topics about this but have yet to get it to work. The output when run manually is:
C:\Temp\Java>ftp -s:c:\\temp\java\\upload.txt 192.168.1.9
Connected to 192.168.1.9.
220 Service Ready for new User
User (192.168.1.9:(none)):
331 Password Needed for Login

230 User jpifer Logged in Successfully
ftp>
ftp>
ftp> cd /ghost/ghost/images
250 Directory successfully changed to "/ghost/ghost/images"
ftp>
ftp> lcd c:\temp\java
Local directory now C:\temp\Java.
ftp>
ftp> put bugsbunny.zip
200 PORT Command OK
150 Opening data connection
226 Transfer Complete
ftp: 370420 bytes sent in 0.04Seconds 9260.50Kbytes/sec.
ftp>
ftp> close
221 Closing Session
ftp>
ftp> bye

When run by any of the three types of code I get this:
C:\Temp\Java>java DBCopy
User (192.168.1.9:(none)): Local directory now C:\temp\Java.




cd /ghost/ghost/images

lcd c:\temp\java

put bugsbunny.zip

close

bye

Below are the three different ways I've tried to capture the data. Anyone give me a hand? I'd like to retrieve it just how it looks when running it manually.

Thanks,
James

public void ftpconnection2() //send file using ftp
{
try
{
Runtime r = Runtime.getRuntime();
Process proc = r.exec("ftp -s:" + DBCopyPath + "\\upload.txt " + ftpServer);
int inp;
String s="";
while ((inp = proc.getInputStream().read()) != -1)
{
s+=(char)inp;
/*System.out.write(inp);this is printing the needed thing on my screen */
}
System.out.println("done \n");
System.out.println(s);
/* This is printing some garbage like 1222232323 etc */
int err;
proc.waitFor();
}catch(Exception e)
{System.out.println("Error"+e);}
}

///////////////
/*
Process p = null;
Runtime r = Runtime.getRuntime();
System.out.println("Run the process");
p = r.exec("ftp -s:c:\\temp\\java\\upload.txt 192.168.1.9");
InputStream iStream = p.getInputStream();
InputStreamReader isReader = new InputStreamReader( iStream );
BufferedReader iReader = new BufferedReader( isReader );
String input = null;

System.out.println( "Standard output : " );
while( ( input = iReader.readLine() ) != null )
{
System.out.println( input );
} } catch (Exception e)
{
System.out.println("Problem with running ftp "+e.getMessage());
}
*/
///////////////////////
/* InputStream in = (Runtime.getRuntime().exec("ftp -s:c:\\temp\\java\\upload.txt 192.168.1.9")).getInputStream();
byte[] arreglo= new byte[200];
int cantidad = in.read(arreglo);
System.out.println(new String(arreglo,0,cantidad));
} catch (IOException ioe){System.out.println(ioe.getMessage());}
*/
/////////////////
 
Hi jamescpp,

I thinks you code work ok itself, but when I tried to resolve this problem I found an very interesting thing: when you login like that the verbose mode is set to off. This is the reason why you can't see the whole server response. I had a command-file like this:

username
password
verbose <--- this sets verbose mode on
ls
get xls.tar

...And just in case, here is my test prog (but i think yours works ok as well):

import java.io.*;
public class FTPExec
{
public static void main(String argv[])
{
FTPExec ftpc = new FTPExec(argv[0]);
ftpc.exec();
}

public FTPExec(String cmdFile_)
{
_cmdFile = cmdFile_;
}

public void exec()
{
try
{
// Execute command
String command = &quot;ftp -s:&quot; + _cmdFile + &quot; ftp.somehost.com&quot;;
Process p = Runtime.getRuntime().exec(command);

InputStream in = p.getInputStream();
BufferedInputStream bis = new BufferedInputStream(in);

byte[] buff = new byte[4096];
int rec = 0;
String input = &quot;&quot;;

while((rec = bis.read(buff,0,4096)) != -1)
{
input = new String(buff,0,rec);
System.out.println(input);
}

}
catch (IOException e) { e.printStackTrace();}
}
private String _cmdFile;
}

-Vepo
 
a redirect, i.e. >&quot;filename&quot;
c:\ftp.bat >&quot;c:\ftp.log&quot;
will direct the output to a file
 
Redirecting to a file does not work when calling the application using exec(). It might if I was calling a batch file, but in this case I call the program directly because of the information I need to send it.

Vepo's response of turning on verbose has solved the problem. Thank you both for your responses.

James
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top