• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Priyha Jootu
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Thanks
Priyha
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13073
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Bill
 
Priyha Jootu
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Thanks,
Priyha.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you expecting the Notepad window to appear on the client or server system?
bear
 
Priyha Jootu
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
Thanks
Priyha
 
Premkumar N
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Priya:

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.
 
Priyha Jootu
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
Thanks,
priyha
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic