File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Why is Runtime.getRuntime().exec() not working in JSP? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Why is Runtime.getRuntime().exec() not working in JSP?" Watch "Why is Runtime.getRuntime().exec() not working in JSP?" New topic

Why is Runtime.getRuntime().exec() not working in JSP?

Priyha Jootu
Ranch Hand

Joined: Sep 13, 2001
Posts: 47
Hi all,
I need to execute a command file from a JSP. I am using Tomcat 4.0.3.
Here is the code :
I just wanted to test if this will work, so tried to do run notepad.exe. But in my actual project, i need to run some other commands from a bat file or cmd file.

I have a html page , when i click a button there , it will call the above jsp , and this jsp will execute the command file.
If i run the above java statements using a simple java program, it works fine, it opens a notepad and explorer window. but if i do the same thing in jsp, why doesn't it work? Is there any security problem? no exception is shown . Is it a problem with tomcat?
I also tried to import the java class in jsp and call the function, but still didnt work.
Please let me know whats the problem, and how to execute command files from jsp.
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13027
It is certainly possible to exec programs on the server side but you have to do it carefullly. You should provide for catching and reporting the various Exceptions that may occur and you have to provide for reading the stdout and stderr streams. Look at the java.lang.Process class for how to get the streams. If you don't read the streams the execution will hang mysteriously.
I strongly suggest that you DONT write this code in a JSP. Do it in a bean or similar helper class so you can test it outside the server environment.
Priyha Jootu
Ranch Hand

Joined: Sep 13, 2001
Posts: 47
Hi Bill,
Thanks for your suggestion, I tried with writing the code in a class and then including it in jsp. Still not working.
Here is the java code :

and in the jsp i have this:

If I run the above java class, it works fine , but if i call the method in the java class through a jsp, it does not work, it returns a exit value of 3, which means not success.
I tried the code directly in jsp(which is not desired, but just for testing), it returns a exit value 128.
whats preventing it in jsp when it works fine in a stand alone java class??
is there anything to do with security issues? please help me with this.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63548

Are you expecting the Notepad window to appear on the client or server system?

[Asking smart questions] [About Bear] [Books by Bear]
Priyha Jootu
Ranch Hand

Joined: Sep 13, 2001
Posts: 47
Basically I don't want notepad to open at all, it is just for testing.
I need to run some other commands ( cvs source management commands).
When i click a button, i need to invoke some commands in cvs that copies files from one directory to another. This is mainly to automate the copy process. Instead of user giving commands to copy certain files from one directory to another, just clicking on a button should do that.
In the example in my previous message, i just checked the taskmanager, the notepad.exe is running in processes, but it is not visible in my system.
Let me know how to do this. I think running a batch file is a solution to this, is there any other approach to this problem?
Premkumar N

Joined: Apr 21, 2001
Posts: 22

You may want to use the following format in your bat file
start notepad.exe -this will start a process in a separate thread

Also: for testing purpose try giving commands like dir.

<a href="" rel="nofollow"></a> <br />Sr.Programmer Analyst ( Bioinformatics)<br />Monsanto Entrprises
Priyha Jootu
Ranch Hand

Joined: Sep 13, 2001
Posts: 47
I tried it using start notepad.exe, but still the same problem.. exit value is 3.
No clue what the problem is , so its getting hard to correct it. Let me know if there is a better solution.
Priyha Jootu
Ranch Hand

Joined: Sep 13, 2001
Posts: 47
One more clue, I used dir and notepad.exe in my batch file. I invoked the jsp and checked the tomcat logs, there i could see the dir contents and notepad.exe . Also in the task manager, notepad.exe is there, but it is not visible.
But i dont want to execute notepad in my project, i just need to execute some cvs source management commands, so i tried some cvs commands in my batch file, i can see that command being invoked in the tomcat logs, but the command doesnt work. no success...
has anybody done similar to this before?? need some help...
I agree. Here's the link:
subject: Why is Runtime.getRuntime().exec() not working in JSP?
jQuery in Action, 3rd edition