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

Posted on 2007-11-26
Medium Priority
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?
Question by:jmarkfoley
  • 3
  • 2
LVL 27

Expert Comment

ID: 20351327
Yes.  Something like this:

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

Expert Comment

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


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

Accepted Solution

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).
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

LVL 27

Assisted Solution

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");

Author Comment

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.
LVL 27

Expert Comment

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.

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Starting a business requires forethought, planning and preparation. As eager as you are to get started with business activities, you need to walk through a few important steps to ensure that your business gets off on the right foot.
Though there are a few manual ways to import PST files to Office 365 , third-party PST to Office 365 import tools are preferred over them due to various reasons.  Consequently, many tools or services are available for the same. Here, we pick the to…
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
If you are looking for an automated solution for backup single or multiple Office 365 user mailboxes to Outlook data file, then you can use Kernel Office 365 Backup & Restore tool. Go through the video to check out the steps to backup single or mult…
Suggested Courses

619 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