aspose file tools*
The moose likes Servlets and the fly likes run unix commands inside Java Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "run unix commands inside Java Servlet" Watch "run unix commands inside Java Servlet" New topic
Author

run unix commands inside Java Servlet

Bhasker Reddy
Ranch Hand

Joined: Jun 13, 2000
Posts: 176
I want to run some unix commands inside java
code.
open up an ftp session
and then pass username
and password
and change it to a particular directory.
put files in there
and quit the ftp session
Does any of you know how to do this ftp


Bhasker Reddy
Bhasker Reddy
Ranch Hand

Joined: Jun 13, 2000
Posts: 176
Can we do this using Runtime class
process p = Runtime.getRuntime().exec(command);
command can be unix commands like
ftp xxx.xxx.xxx.com,
username,
password,
cd to directory
put file
quit
Bhasker Reddy
Ranch Hand

Joined: Jun 13, 2000
Posts: 176
Process p = Runtime.getRuntime().exec("ftp");
Process p1 = Runtime.getRuntime().exec("open servername.xxx.xxx.com");
Process p2 = Runtime.getRuntime().exec("USERNAME");
Process p3 = Runtime.getRuntime().exec("PASSWORD");
Process p4 = Runtime.getRuntime().exec("cd ../../oraexp/ORACLE/xxxx/export/XML/");
Process p5 = Runtime.getRuntime().exec("put 23005.xml");
Process p6 = Runtime.getRuntime().exec("quit");
Process p7 = Runtime.getRuntime().exec("quit");
I tried to Run these, but i am getting errors
I am on a windows 2000 box, and i am trying to
ftp to a oracle unix server.
Guy Allard
Ranch Hand

Joined: Nov 24, 2000
Posts: 776
Can you run the ftp from a Win2K command line?
Make sure that works first.
Then be a little more specific on 'errors' please.
Regards, Guy
Eric lau
Greenhorn

Joined: Jan 21, 2002
Posts: 27
I try the exec() but i get error = num
where can I get the description of the error with the num?
Originally posted by Bhasker Reddy:
Process p = Runtime.getRuntime().exec("ftp");
Process p1 = Runtime.getRuntime().exec("open servername.xxx.xxx.com");
Process p2 = Runtime.getRuntime().exec("USERNAME");
Process p3 = Runtime.getRuntime().exec("PASSWORD");
Process p4 = Runtime.getRuntime().exec("cd ../../oraexp/ORACLE/xxxx/export/XML/");
Process p5 = Runtime.getRuntime().exec("put 23005.xml");
Process p6 = Runtime.getRuntime().exec("quit");
Process p7 = Runtime.getRuntime().exec("quit");
I tried to Run these, but i am getting errors
I am on a windows 2000 box, and i am trying to
ftp to a oracle unix server.


I come from China
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

yes, you can ftp from Windows command line.

What is 'num'? And by exact error, if there is a stack trace, please provide it.
Eric lau
Greenhorn

Joined: Jan 21, 2002
Posts: 27
such as
what is the means of error=2 and where can I get all description of the error code?
java.io.IOException: CreateProcess: guest error=2
at java.lang.Win32Process.create(Native Method)
at java.lang.Win32Process.<init>(Win32Process.java:64)
at java.lang.Runtime.execInternal(Native Method)
at java.lang.Runtime.exec(Runtime.java:272)
at java.lang.Runtime.exec(Runtime.java:195)
at java.lang.Runtime.exec(Runtime.java:152)
at testThread.Untitled1.main(Untitled1.java:89)

Originally posted by Mike Curwen:
yes, you can ftp from Windows command line.

What is 'num'? And by exact error, if there is a stack trace, please provide it.
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
The way in which you are using exec won't work. Exec only works on full commands (e.g. invoking programs) and what you're actually trying to do (once you invoke FTP) is send input on STDIN to the process. That's not as easy as it sounds.
Instead, why not try a google search and look to see if anyone has written Java FTP classes? I did that and found the following:
http://www.geocities.com/beapetrovicova/
http://www.enterprisedt.com/downloads/ftp.html
(Pointed to, by the way from the wonderful http://www.gnu.org/software/java/java-software.html)
http://www.cs.albany.edu/~casper/java/
http://www.ibiblio.org/javafaq/books/secrets/secretsexamples/

ANY of these should meet your needs.
Kyle


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
Robert Gagliardo
Greenhorn

Joined: Nov 15, 2001
Posts: 24
Originally posted by Bhasker Reddy:
Can we do this using Runtime class
process p = Runtime.getRuntime().exec(command);
command can be unix commands like
ftp xxx.xxx.xxx.com,
username,
password,
cd to directory
put file
quit

you should be doing something like this.
InputStream ip = p.getInputStream();
and write the ftp commands to the stream
Bhasker Reddy
Ranch Hand

Joined: Jun 13, 2000
Posts: 176
Do you mean to say that i need to write ftp commands in input stream and put the input stream
in the Runtime.exec(inputstream).
can you be more specific
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: run unix commands inside Java Servlet