java Runtime.exec in linux 8
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   nandan_nanduri
Posted On:   Wednesday, June 4, 2003 10:01 PM

This is code i am executing under linux 8 and i am using
bash shell.After excuting the code nothing is happening.

can anybody help me.



class hello

{

public static void main(String args[]) throws Exception


{


String[] cmd = {"/bin/bash", "-c", "/bin/ls"};

Runtime.getRuntime().exec(cmd);

}

}

Re: java Runtime.exec in linux 8

Posted By:   Simon_Ablett  
Posted On:   Thursday, June 5, 2003 03:48 AM

I would say that the reason you are not seeing anything is to do with your stdout / stderr streams getting redirected (i.e. to the bash shell). If you change your code to something akin to the following.


public class T
{
public static void main(String args[]) throws Exception
{
String[] cmd = {"/bin/bash", "-c", "echo Simon > ./log.log"};
Runtime.getRuntime().exec(cmd);
}
}

Then you will see that, assuming that the access rights are correct, a file called 'log.log' has been created conatining the string 'simon'. This implies that the commands are being run correctly. Now try the following.


public class T
{
public static void main(String args[]) throws Exception
{
String[] cmd = {"/bin/bash", "-c", "/bin/ls > ./log.log"};
Runtime.getRuntime().exec(cmd);
}
}

Now the file contains the results of your 'ls' operation.

Regards.
About | Sitemap | Contact