java調用shell腳本,解決權限問題


 /**
     * 解決了 參數中包含 空格和腳本沒有執行權限的問題
     * @param scriptPath 腳本路徑
     * @param para 參數數組
     */
    private void execShell(String scriptPath) {
        try {
            //解決腳本沒有執行權限
            ProcessBuilder builder = new ProcessBuilder("/bin/chmod", "755",scriptPath);
            Process process = builder.start();
            process.waitFor();

            Process ps = Runtime.getRuntime().exec(scriptPath);
            ps.waitFor();

            BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
            StringBuffer sb = new StringBuffer();
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line).append("\n");
            }
            //執行結果
            String result = sb.toString();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM