?
Solved

Is there a way to run "native" commands from jsp?

Posted on 2007-11-26
6
Medium Priority
?
645 Views
Last Modified: 2008-02-01
I have tomcat 6.0 on linux. Is there a way to run native commands such as "ls > ls.out" or whatever?
0
Comment
Question by:jmarkfoley
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 20351327
Yes.  Something like this:

Process p = Runtime.getRuntime().exec("ls > ls.out");
0
 
LVL 8

Expert Comment

by:malfunction84
ID: 20351440
I don't believe that will work.  Runtime.exec() is NOT a command line.  The following article explains why:

http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=4

(It's written in the context of Windows, but it applies to Linux as well.)
0
 
LVL 8

Accepted Solution

by:
malfunction84 earned 1000 total points
ID: 20351473
Basically, to do what you want to do safely, you must account for several things:
- The Process is in its own Thread.
- The Process may produce output which must be consumed.
- This is not a CLI, so redirects must be handled manually (unless you're calling a shell script).
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Assisted Solution

by:mrcoffee365
mrcoffee365 earned 1000 total points
ID: 20351825
Ah -- you're right.  The > redirect of stdout won't run.  So put it in a .csh file named ls.csh, and run that:

Process p = Runtime.getRuntime().exec("ls.csh");
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 20352519
I went ahead and tried the piping command and, as predicted, it didn't work. I also tried Runtime.getRuntime().exec("/bin/sh -c 'echo Howdy | /usr/bin/mail -s testing me@mydomain.com'   >/dev/null 2>&1"); and that didn't work either; which was frankly surprising. Perhaps the stdout/stderr redirects messed it up. I removed those, but the return status was '2', and no message was sent. I may experiment further. I'll have to look up the '2' status.

However, putting it all in a shell script did work fine.
0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 20353178
Right -- if you look at the article malfunction84 gave in a post above, you'll see the explanation.  You can't redirect stderr and stdout -- you have to handle them programmatically in your Java program.  So doing "/bin/sh -c 'xxxxx'" is fine, but >/dev/null is not.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question