This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

exec command not working  RSS feed

 
govind bhardwaj
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

In future, while posting code, please UseCodeTags
 
Campbell Ritchie
Sheriff
Posts: 55292
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where does it return null? There aren't any return statements in what you posted.
 
govind bhardwaj
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the thing inside the while loop doesn't seem to work.
So i wrote down

String answer;
answer=buf.readLine();
out.write(command+answer);

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

 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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."

Bill
 
Guido Granobles
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It may not completely apply to this topic however this is a good article about using Runtime.exec().
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!