problem when calling unix command from jsp

S

siujean

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
 
M

Manish Pandit

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
 
M

Manish Pandit

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
 
L

Lord0

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
 
B

Babu Kalakrishnan

siujean said:
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

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
 
M

Mark Space

siujean said:
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

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.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Manish said:
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);

That is very good point.

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

Arne
 
G

Gordon Beaton

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().
 
M

Mark Space

Gordon said:
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. ^_^
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top