weiqiuhong's profile我的地盤PhotosBlogLists Tools Help

Blog


    November 09

    用java在不同操作系统下安装PERL组件

    需求:用JAVA语言实现WINDOW和LINUX系统下自动安装PERL组件的功能。
     public void execNormalInstall(String modulename) {
            Runtime runTime = Runtime.getRuntime();
            Process process = null;
            try {
                if (System.getProperty("os.name").toUpperCase().indexOf("WINDOWS") >= 0) {
                    modulename = modulename.replaceAll("::", "-");
                    String path = CorePlugin.getDefault().getPreferenceStore().getString(ITalendCorePrefConstants.PERL_INTERPRETER);//安装PERL的目录 ex:E:\Perl\bin
                    path = path.substring(0, path.lastIndexOf("\\"));
                    process = runTime.exec("cmd /c start /D" + path + "\\" + " ppm install " + modulename);
                  // process = runTime.exec("cmd /c start /D" + path + "\\" + " /B ppm install " + modulename);// "/B"不打开DOS窗口
                } else if (System.getProperty("os.name").toUpperCase().indexOf("LINUX") >= 0) {
                    String command = "perl -MCPAN -e 'install " + modulename + "'";
                    openTerminal(command, process);
                }
            } catch (Exception e) {
                MessageBoxExceptionHandler.process(e);
            }
        }
      public static void openTerminal(String command, Process process) {
            String terminal = System.getenv("TERM");
            try {
                process = Runtime.getRuntime().exec(new String[] { terminal, "-e", command + "; $SHELL" });
                process.waitFor();
            } catch (IOException e) {
                System.out.println(e.getMessage());
            } catch (InterruptedException e) {
                // TODO: handle exception
            }
    当然如果是linux,需要安装gcc的编译器,之前不懂PERL,也没用过LINUX,浪费好几天,在LINUX下还有个权限问题。