mardi 23 juin 2015

NetBeans Java exécuter un programme.exe ou une ligne de commande

Pour exécuter une ligne de commande, voici comment faire:

File destDir = jFileChooser_Dest.getCurrentDirectory();
       
        String del_cmd = "cmd /c rmdir /S /Q " + destDir;
        String create_cmd= "cmd /c mkdir " + destDir;
        
        try{
                    
            Process rt = Runtime.getRuntime().exec(del_cmd);
           
            StreamGobbler errorGobbler = new  
            StreamGobbler(rt.getErrorStream(), "ERROR");             
             
            // any output? 
            StreamGobbler outputGobbler = new  
            StreamGobbler(rt.getInputStream(), "OUTPUT"); 
                 
            // kick them off 
            errorGobbler.start(); 
            outputGobbler.start();
          
            rt.waitFor();
            //Process pr = rt.exec();
          
        }catch (IOException ex){        
            //jTextField_Statut.setText(ex.toString());
            Logger.getLogger(MedFilm_MainView.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InterruptedException ex) {
            Logger.getLogger(MedFilm_MainView.class.getName()).log(Level.SEVERE, null, ex);
        }
       
        try{
                    
            Process rt = Runtime.getRuntime().exec(create_cmd);
           
            StreamGobbler errorGobbler = new  
            StreamGobbler(rt.getErrorStream(), "ERROR");             
             
            // any output? 
            StreamGobbler outputGobbler = new  
            StreamGobbler(rt.getInputStream(), "OUTPUT"); 
                 
            // kick them off 
            errorGobbler.start(); 
            outputGobbler.start();
          
            rt.waitFor();
            //Process pr = rt.exec();
          
        }catch (IOException ex){        
            //jTextField_Statut.setText(ex.toString());
            Logger.getLogger(MedFilm_MainView.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InterruptedException ex) {
            Logger.getLogger(MedFilm_MainView.class.getName()).log(Level.SEVERE, null, ex);
        }
       
        jFileChooser_Dest.rescanCurrentDirectory();
    }       

Aucun commentaire:

Publier un commentaire