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

exec command not working

govind bhardwaj

Joined: Sep 17, 2010
Posts: 2
The following code was working fine in a separate program, but when I inserted into my Servlet code, it always returns a null. Can anyone please tell where am I going wrong.

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10687

Welcome to the Ranch.

In future, while posting code, please UseCodeTags

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 41573
Where does it return null? There aren't any return statements in what you posted.
govind bhardwaj

Joined: Sep 17, 2010
Posts: 2
the thing inside the while loop doesn't seem to work.
So i wrote down

String answer;

and what i at the other end was
"java add 2 3null"

William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 12912
If the process failed at the start and only wrote to the stderr stream, then you would get null from the buf.readLine()

Does the Java program you are attempting to run have some assumption about the "current directory"? Remember, in the servlet environment you have no control over the "current directory."

Guido Granobles

Joined: Sep 11, 2010
Posts: 22
If you are using Tomcat as a servlet container then the Java classes that you want execute should be in the ROOT directory. That is to say in something like $TOMCA_HOME/webapps/yourWebApp/ROOT. If your classes are packaged then you should have the directory in there: ROOT/namePackage1/myClass
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

It may not completely apply to this topic however this is a good article about using Runtime.exec().

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
I agree. Here's the link:
subject: exec command not working