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!

Runtime.getRuntime() to change directory and then list it

807589Jul 22 2008 — edited Jul 22 2008
Hi,

i have problem using Runtime.getRuntime() to change working directory and then list it
anyone know how to do it

basically i want 2 things to be done
1. cd c:/exploded-temp
2. dir /w

below is my partial code
	 cmd[0] = "cmd.exe" ;
                 cmd[1] = "/C" ;
                 cmd[2] = "cd C:/exploded-temp";
                     
            
            Runtime rt = Runtime.getRuntime();
                       Process proc = rt.exec(cmd);
            
              
            // any error message?
            StreamGobbler errorGobbler = new      StreamGobbler(proc.getErrorStream(), "ERROR");            
            
            // any output?
            StreamGobbler outputGobbler = new 
                StreamGobbler(proc.getInputStream(), "OUTPUT");
                
            // kick them off
            errorGobbler.start();
            outputGobbler.start();
            
                                              
            // any error???
            int exitVal = proc.waitFor();
            System.out.println("ExitValue: " + exitVal);   
            
            cmd[0] = "cmd.exe" ;
            cmd[1] = "/C" ;
            cmd[2] = "dir /w";
            proc = rt.exec(cmd);
            
 errorGobbler = new      StreamGobbler(proc.getErrorStream(), "ERROR");            
            
            // any output?
             outputGobbler = new 
                StreamGobbler(proc.getInputStream(), "OUTPUT");
                
            // kick them off
            errorGobbler.start();
            outputGobbler.start();
but the code above seems doesnt work
it only list whatever directory i run this java class from

anyone know how to solve it?
thanks
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 19 2008
Added on Jul 22 2008
6 comments
2,716 views