Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

run unix commands inside Java Servlet

 
Bhasker Reddy
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 176
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Robert Gagliardo
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 176
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic