Solved

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

Posted on 2007-11-26
6
644 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 250 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
Technology Partners: 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 250 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

Technology Partners: 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!

Question has a verified solution.

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

Multi-threading long-running processes can have a significant increase in overall performance and drastically decrease over time it takes for a process to complete. Unfortunately, not all applications support native multi-threading, some by design a…
Popular third-party chat platforms like Slack, Discord, and Telegram are just a few of the many new productivity applications that are being hijacked by cybercriminals to create command-and-control (C&C) communications infrastructures for their malw…
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

728 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