Solved

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

Posted on 2007-11-26
6
639 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 26

Expert Comment

by:mrcoffee365
Comment Utility
Yes.  Something like this:

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

Expert Comment

by:malfunction84
Comment Utility
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 250 total points
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 26

Assisted Solution

by:mrcoffee365
mrcoffee365 earned 250 total points
Comment Utility
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
Comment Utility
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 26

Expert Comment

by:mrcoffee365
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
Find out what Office 365 Transport Rules are, how they work and their limitations managing Office 365 signatures.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now