File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes How to invoke 'exe' in JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to invoke Watch "How to invoke New topic
Author

How to invoke 'exe' in JSP

vaibhav punekar
Ranch Hand

Joined: Jan 20, 2001
Posts: 134
Hello guys,
I m working on a project that need to invoke exe in JSP at runtime.
I m using Runtime.getRuntime(),exec("*.exe").It is working fine in standalone class but throws IOException while I use the instance in JSP.I tried using bean also but to no help.
In win98 the tomcat server itself shuts down when I try to invoke this class in JSP.I don't know what is the problem?
Please help in invoking .exe in JSP.Thank you.


VAIBHAV <BR>SCJP
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12809
    
    5
Exactly which IOException is thrown?
Since a JSP is nothing but a fancy servlet, you should be able to execute a program with one of the exec methods.
Val Dra
Ranch Hand

Joined: Jan 26, 2001
Posts: 439
aren't some certain promissions required because you are not supposed to access the underlying file system of the user ?


Val SCJP <BR>going for SCJD
liz munroe
Greenhorn

Joined: Mar 01, 2001
Posts: 5
Make sure that whatever Tomcat is running as (as a service or as system?) has permission to run your exe.
Here is some sample code. I am using unix, so for the variable yourCommand, instead of "/home/liz/test.pl" put in "C:\path\to\your\exe.exe" and uncomment the error and output lines if you need those.
If you need to pass in parameters to your exe, define them and then add them to the fullCommand array. i.e.
String yourCommand = "C:\path\to\your\exe.exe";
String yourParam = whatever;
String[] fullCommand = { yourCommand, yourParam };
You can't use fullCommand = "yourCommand yourParam";
Also, if the path to your exe has spaces in it, you need to put it in quotes. i.e.
String yourCommand = "\"c:/program files/your.exe\"";
I got most of the code from Jason Hunter's Java Servlet Programming book (first edition) in Chapter 13.
good luck!
***************************
<%@ page import="java.io.*" %>
<html>
<head>
<title>Exec example</title>
</head>
<body>
<%
String yourCommand = "/home/liz/test.pl";
String[] fullCommand = { yourCommand };
try {
Process p = Runtime.getRuntime().exec(fullCommand);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
//read the output from the command
//out.println("Here is the standard output of the command:<br>");
//String s = null;
//while ((s = stdInput.readLine()) != null) {
// out.println(s + "<br>");
//}
// read any errors from the attempted command
//out.println("<P>Here is the standard error of the command (if any):<br>");
//while ((s = stdError.readLine()) != null) {
// out.println(s + "<br>");
//}
//int exitVal = p.waitFor();
//out.println("Process exit value = " + exitVal);
} catch (IOException ioe) {
System.err.println("\n\n\nIOException: " + ioe.toString());
}
%>
</body>
</html>
Andrew Shafer
Ranch Hand

Joined: Jan 19, 2001
Posts: 338

I often use Runtime.exec() in JSP running inside Tomcat, but everything is on Linux so its not exactly the same setup.
I suspect based on the exception and what you have told us that the code has some other problem.
I suggest posting the code if you can and also the details you are getting with the exception.


!_I_Know_Kung_Fu_!
vaibhav punekar
Ranch Hand

Joined: Jan 20, 2001
Posts: 134
Hi,
I don't know what was the problem But on monday when I came in the office I tried it once again.It is working fine.I don't know what the problem was.I tried using
Runtime.getRuntime().exec("java exeInvoker");---in JSP
and that class invokes .exe.It worked fine.But it is not using the advantage of servlets over cgi.It will create an instace per request I think,instead of using multiple threads for single instace.Is it the right way to do?
But now I ve made again
Runtime.getRuntime().exec("cpp.exe");It is working fine.
Is their any discrepancy while invoking exe?
Thanks.
vaibhav punekar
Ranch Hand

Joined: Jan 20, 2001
Posts: 134
Hi,
I don't know what was the problem But on monday when I came in the office I tried it once again.It is working fine.I don't know what the problem was.I tried using
Runtime.getRuntime().exec("java exeInvoker");---in JSP
and that class invokes .exe.It worked fine.But it is not using the advantage of servlets over cgi.It will create an instace per request I think,instead of using multiple threads for single instace.Is it the right way to do?
But now I ve made again
Runtime.getRuntime().exec("cpp.exe");It is working fine.
Is their any discrepancy while invoking exe?
Thanks.
 
jQuery in Action, 2nd edition
 
subject: How to invoke 'exe' in JSP