running unix script from java app

S

sight

String[] cmd = {"/bin/sh", "-c", "ls > hello"};
Runtime.getRuntime().exec(cmd);
Following code doesn't create a file hello when app is run on unix?!??How
can i start unix script from java app?I'm using cygwin instead of Unix
 
O

Oliver Wong

sight said:
String[] cmd = {"/bin/sh", "-c", "ls > hello"};
Runtime.getRuntime().exec(cmd);
Following code doesn't create a file hello when app is run on unix?!??How
can i start unix script from java app?I'm using cygwin instead of Unix

Did you try:

String[] cmd = {"/bin/sh", "-c", "ls" "> hello"};

or

String[] cmd = {"/bin/sh", "-c", "ls" ">" "hello"};

or other variants? I believe it's the shell and not the ls program, which
takes care of redirecting output.

- Oliver
 
?

=?ISO-8859-2?Q?Dra=BEen_Gemi=E6?=

sight said:
String[] cmd = {"/bin/sh", "-c", "ls > hello"};
Runtime.getRuntime().exec(cmd);
Following code doesn't create a file hello when app is run on unix?!??How
can i start unix script from java app?I'm using cygwin instead of Unix

I think that the problem is with "ls > hello" part. Runtime.exec()
method creates a Process object instance. Java captures stdin,
stdout and stderr streams, so you are not able to redirect. The
command is probably executed, but you are not able to see the result.

Check the java.lang.Process documentation.

DG
 
C

Chris Uppal

sight said:
String[] cmd = {"/bin/sh", "-c", "ls > hello"};
Runtime.getRuntime().exec(cmd);
Following code doesn't create a file hello when app is run on unix?!??How
can i start unix script from java app?I'm using cygwin instead of Unix

Your approach would, I think, be correct on Unix. You are passing the right
string to the shell to be interpreted as a command. I suspect the problem is
with Cygwin, or more accurately, with the fact that Java doesn't know about
Cygwin. So the first parameter:
"/bin/sh"
with attempt to find
.\bin\sh.exe
in a subdirectory of whatever "current directory" the Windows process thinks it
is in. You may get better results with:
"C:/cygwin/bin/sh"
(replacing the actual path according to where you have actually installed
Cygwin).

-- chris
 
?

=?ISO-8859-2?Q?Dra=BEen_Gemi=E6?=

Oliver said:
or other variants? I believe it's the shell and not the ls program,
which takes care of redirecting output.

That is, in fact, correct. In UNIX, child process, by default, inherits
parents file descriptors. That means that paren program arranges the
descriptors before forking child process.

Check dup, dup2, open and close system calls.

DG
 

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,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top