?
Solved

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

Posted on 2007-11-26
6
Medium Priority
?
648 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
  • 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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

Most folks would know the basics of how Dropbox works, so that’s not the purpose of this article. Security is what it’s all about, so here I’ll share how I choose to secure my Dropbox Account and the Data it contains.
Over time, the online landscape has altered considerably, but that’s nothing compared to the up-and-coming trends that will shape the web design industry in the coming year. Keep reading to find out which trends will shape B2B web design in 2018.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

840 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