Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2006

Tip: Looking for answers? Try searching our database.

problem when calling unix command from jsp

Thread view: 
siujean - 13 Sep 2006 17:22 GMT
Dear all,

I want to list all the file in a unix directory and display in jsp.
And the directory have below files:
-tmp1.txt
-tmp2.txt
-log.txt
-blog.txt

I used the runtime object and Runtime.exec ('ls -lrt') which can get
the file list suceesful.
But when i use Runtime.exec('ls -lrt tmp*) , then it get nothing

Pls kindly help.
Tks
Manish Pandit - 13 Sep 2006 17:41 GMT
Why dont you use java API to do the same? Any particular reason for
using shell script?

IMO, using java.io.File API will keep things consistent far easy to
debug.

File f = new File("path/to/your/folder");
File[] files = f.listFiles();
//iterate through files, print names..

-cheers,
Manish
Manish Pandit - 13 Sep 2006 17:56 GMT
On another thought, if you want to pass arguments to the command, you
should be using an array :

     String[] command = new String[2];
     command[0]="ls -lrt";
     command[1] = "tmp*";
     Runtime.exec(command);

-cheers,
Manish

> Dear all,
>
[quoted text clipped - 11 lines]
> Pls kindly help.
> Tks
Lord0 - 13 Sep 2006 18:24 GMT
I agree with Manish. I would use java.io.File to get the files in the
dir and then pass the List (or whatever) to the JSP. Then if I was
feeling funky use JSTL to iterate over the List in the JSP.

Cheers

Lord0
Arne Vajhøj - 13 Sep 2006 20:22 GMT
> On another thought, if you want to pass arguments to the command, you
> should be using an array :
[quoted text clipped - 3 lines]
>       command[1] = "tmp*";
>       Runtime.exec(command);

That is very good point.

Commands and arguments in one string usually works
on windows and fails on Unix/Linux.

Arne
Babu Kalakrishnan - 13 Sep 2006 19:04 GMT
> Dear all,
>
[quoted text clipped - 8 lines]
> the file list suceesful.
> But when i use Runtime.exec('ls -lrt tmp*) , then it get nothing

In addition to what the other posters said, you also need to
remember that using relative paths from within web applications
can give you surprises. Different servlet containers have
different views about what the "current directory" is.

BK
Mark Space - 13 Sep 2006 19:59 GMT
> Dear all,
>
[quoted text clipped - 11 lines]
> Pls kindly help.
> Tks

I strongly agree that java.io.File is the way to go.  Works on any
platform, whether Unix, Windows, Solaris or what have you.

However, just FYI, I'm pretty sure that wildcard expansion is done by
the shell, not by commands like ls.  Hence, you need to invoke bash (or
whatever your favorite shell is):

Runtime.exec('bash -c "ls -d tmp*"');

If you don't know why the -d is there, try "mkdir test", "touch
test/oops" in the above example directory, then do "ls te*" and see what
you get.
Gordon Beaton - 14 Sep 2006 07:37 GMT
> However, just FYI, I'm pretty sure that wildcard expansion is done by
> the shell, not by commands like ls.  

Absolutely correct.

> Hence, you need to invoke bash (or whatever your favorite shell is):
>
> Runtime.exec('bash -c "ls -d tmp*"');

Almost right. Since you need to group the command "ls -d tmp*" into a
single argument for the shell, you can't use this version of
Runtime.exec() (which passes the command line to StringTokenizer,
which doesn't handle quoting).

This is the way:

String[] cmd = { "/bin/sh", "-c", "ls -d tmp*" };
Process p = Runtime.getRuntime()exec(cmd);

That said, I full agree with the other posters who have suggested that
you can do this directly with File.listFiles().

Signature

[ don't email me support questions or followups ]
g o r d o n  +  n e w s  @  b a l d e r 1 3 . s e

Mark Space - 14 Sep 2006 17:38 GMT
>> Runtime.exec('bash -c "ls -d tmp*"');
>
> Almost right. Since you need to group the command "ls -d tmp*" into a
> single argument for the shell, you can't use this version of
> Runtime.exec() (which passes the command line to StringTokenizer,
> which doesn't handle quoting).

I didn't actually write any Java to test this; it worked from the
command line.  Thanks for the points to the proper way to do it. ^_^


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.