Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to execute unix commands through Windows/cygwin using Java

864501Jul 19 2011 — edited Jul 19 2011
I am trying to accomplish two things:

1. I am running cygwin on Windows7 to execute my unix shell commands and I need to automate the process by writing a Java app. I already know how to use the windows shell through Java using the 'Process class' and 'Runtime.getRuntime().exec(cmd /c dir)'. I need to be able to do the same with unix commands: i.e.: 'ls -la' and so forth. What should I look into ?
I have tried running 'Runtime.getRuntime().exec("C:\\cygwin\\bin\\mintty.exe -e ls -la")' or 'Runtime.getRuntime().exec("C:\\cygwin\\bin\\bash.exe -c ls -la");', but for some reason, both mintty and bash end up not recognizing basic commands like the 'ls' when they are called this way !?

2. Is there a way to remember a shell's state? explanation: when I use: 'Runtime.getRuntime().exec(cmd /c dir)' I always get a listing of my home directory. if I do 'Runtime.getRuntime().exec(cmd /c cd )' and then do 'Runtime.getRuntime().exec(cmd /c dir)' again, I will still get the listing of my home folder. Is there a way to tell the process to remember its state, like a regular shell would?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 16 2011
Added on Jul 19 2011
3 comments
1,563 views