For sure, as Milt says it could easily be a permissions problem. I had a
similar situation recently on an NT-based web server running ServletExec
where which couldn't recognise a mapping to a Netware drive for an ODBC
connection even though I had an explicit mapping to it externally and
ServletExec was running under an NT service which was logged in as me!
If your platform is Unix, things will be different but the principle
could be the same.
Another issue here could be file redirection. If you expect to see the
stdout of an NT process back to Java's stdout, or try to send a
subprocess stdout to a file using > operator it fails for no apparent
reason. What's actually needed is to redirect the stdout and stderr from
within Process to either System.out or a filestream that is captured by
a pair of independent threads. There's a Java class called StreamGobbler
(available on one of the Java forums) which I used to solve this problem
some time ago and you may also find it useful. Apart from allowing
independent trapping of stdout/stderr it also contains an alternate
constructor which allows one to redirect the output stream to a file. To
save you time, examine the following example code which uses this method
for trapping ftp errors. HTH Austin
Wrapper code (in your servlet or wherever):
-------------------------------------------
// This will be the input to the ftp exe, which sends files from
NT to Unix.
Process p = Runtime.getRuntime().exec("ftp -i -n");
StreamGobbler errorGobbler = new
StreamGobbler(p.getErrorStream(), "FTP stderrr");
StreamGobbler outputGobbler = new
StreamGobbler(p.getInputStream(), "FTP stdout");
errorGobbler.start();
outputGobbler.start();
PrintWriter toFTP = new PrintWriter(p.getOutputStream(), true);
toFTP.println("open " + server); //
toFTP.println("user " + GemaLoginDialog._ftpSourceUserID + " "
+ GemaLoginDialog._ftpSourcePassword);
System.out.println("Setting remote directory: cd " +
remotePath);
toFTP.println("cd " + remotePath);
toFTP.println("lcd " + localPath );
if (sending)
toFTP.println("mput " + filespec);
else
toFTP.println("mget " + filespec);
toFTP.println("quit");
// trap exit code for FTP process
exitValue = p.waitFor() ;
// wait up to 10 secs for stdout/stderr threads to finish before
shutting them down
errorGobbler.join(10000);
outputGobbler.join(10000);
// trap stdout, stderr reasons for failure
if(exitValue != 0 || !outputGobbler.statusOK() ||
!errorGobbler.statusOK()) {
if (!outputGobbler.getError().equals("")) {
System.out.println("FTP failed: " +
outputGobbler.getError());
SwingUtilities.invokeLater(new UpdateTable("[FTP] FTP
failed: "+ outputGobbler.getError()));
}
else if (!errorGobbler.getError().equals("")) {
System.out.println("FTP failed: " +
errorGobbler.getError());
SwingUtilities.invokeLater(new UpdateTable("[FTP] FTP
failed: "+ errorGobbler.getError()));
}
else {
System.out.println("FTP failed - DOS error
code="+exitValue);
SwingUtilities.invokeLater(new UpdateTable("[FTP] FTP failed
- DOS error code="+exitValue));
}
return false;
}
toFTP.close();
System.out.println("FTP done.");
StreamGobbler class (adapted):
------------------------------
import java.util.*;
import java.io.*;
public class StreamGobbler extends Thread
{
InputStream is;
String type;
OutputStream os;
boolean ok = true ;
boolean finalise = false ;
String reason = "";
public StreamGobbler(InputStream is, String type)
{
this(is, type, null);
}
public StreamGobbler(InputStream is, String type, OutputStream
redirect)
{
this.is = is;
this.type = type;
this.os = redirect;
}
public boolean statusOK() {
return ok ;
}
public boolean isFinalised() {
return finalise ;
}
public String getError() {
return reason ;
}
public void run()
{
try
{
PrintWriter pw = null;
if (os != null)
pw = new PrintWriter(os);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null)
{
if (pw != null)
pw.println(line);
// echo stream output
System.out.println(type + ": " + line);
if (line.indexOf("Unknown host") > -1
|| line.indexOf("Login incorrect") > -1
|| line.indexOf("No such file or directory") > -1
|| line.indexOf("File not found") > -1
|| line.indexOf("Permission denied") > -1
) {
ok = false ;
reason = line ;
finalise = true ;
}
if (line.indexOf("?Invalid command") > -1)
finalise = true ;
}
if (pw != null)
pw.flush();
if (!ok) {
this.interrupt();
finalise = true ;
}
} catch (IOException ioe)
{
ioe.printStackTrace();
finalise = true ;
}
}
}
-----Original Message-----
From: A mailing list for discussion about Sun Microsystem's Java Servlet
API Technology. [mailto:[EMAIL PROTECTED] On Behalf Of Sam
Seaver
Sent: 21 March 2003 20:05
To: [EMAIL PROTECTED]
Subject: Re: unix command
>hi there
>I am trying to execute a unix command from servlet. Command woks fine
>when i try to get output on to terminal and this is the code for that:
>
>Process proc=null;
> Runtime rt = Runtime.getRuntime();
> proc = rt.exec(new String[]
>{"/vol1/people/dinesh/htblast/blockhead_64","-stdout","/vol1/people/din
>esh/htblast/testfile.fsa","/vol1/people/dinesh/htblast/rules_file","1"}
);
>but when i try to redirect it to a file it is not working ,,though it
works
>when i execute this command from command line in unix shell.here is the
>code for that:
>Process proc=null;
> Runtime rt = Runtime.getRuntime();
> proc = rt.exec(new String[]
>{"/vol1/people/dinesh/htblast/blockhead_64","-stdout","/vol1/people/din
esh/htblast/testfile.fsa","/vol1/people/dinesh/htblast/rules_file","1","
>","test2.out"});
>where test2.out is the file where i want my output.
>i have tried giving the absolute path of test2.out but command didnt
write
>to it.
>
I tried to use the redirect arrow in a command from a bean, but that
didn't work. I think it's easier and safer to actually capture the
output in the outputstream, ie: BufferedReader br = new
BufferedReader(new OutputStreamReader(proc.getOutputStream());
and then while reading it, write it to file...
S
_________________________________________________________________
Protect your PC - get McAfee.com VirusScan Online
http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963
________________________________________________________________________
___
To unsubscribe, send email to [EMAIL PROTECTED] and include in the
body of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html