Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to invoke 'exe' in JSP

 
vaibhav punekar
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 439
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
aren't some certain promissions required because you are not supposed to access the underlying file system of the user ?
 
liz munroe
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
vaibhav punekar
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic