Java执行shell命令或cmd命令

执行命令加前缀

cmd命令 : cmd /c

shell命令: shell -c

publicstaticvoid exeCmd(String commandStr) throws Exception { 

            BufferedReader br = null

            try

                Process p = Runtime.getRuntime().exec(commandStr); 

                br = new BufferedReader(new InputStreamReader(p.getInputStream())); 

                String line = null

                StringBuilder sb = new StringBuilder(); 

                while ((line = br.readLine()) != null) { 

                    sb.append(line + "\n"); 

                } 

                System.out.println(sb.toString()); 

            } catch (Exception e) { 

                throw e;

            }  

            finally 

            { 

                if (br != null

                { 

                    try

                        br.close(); 

                    } catch (Exception e) { 

                        throw e;

                    } 

                } 

            } 

        }


你可能感兴趣的:(java)