如题,以下是Postgresql数据库备份代码需要执行批处理,可发现发布后tomcat以控制台方式启动的话代码正常运行(可执行批处理弹出backup.bat窗口待输入密码),若以服务方式启动的话则无法执行批处理backup.bat窗口无法弹出,不知那里出现问题,问问大家要如何解决??try {
System.out.println("runtime");
String cmd = "cmd.exe /c start " + "D:\\SLDDataCenter\\Postgresql\\backup.bat" ;
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader br = new BufferedReader( new InputStreamReader(p.getInputStream()));
System.out.println("waiting for password...");
            while((br.readLine())!=null){
             System.out.println("br: "+br.readLine());
            }
while (true){
                if(p.waitFor() == 0)break;
             }
p.destroy();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

解决方案 »

  1.   


        //==========================数据库备份代码 开始===========================//
    BufferedWriter mBufWriter = null;

    //建立一个BackupDb.bat文件
    FileWriter fileWriter = new FileWriter("BackupDb.bat");
    mBufWriter = new BufferedWriter(fileWriter); //转到PostgreSQL安装的根目录
    String batLine1=getText("database.postgresPath1");

    //转到PostgreSQL下pg_dump.exe文件的路径
    String batLine2="cd " + getText("database.postgresPath2");
    String batLine4="CLS";

    //设置数据库密码,为了跳过后面输入密码的过程
    String batLine3="set PGPASSWORD=" + getText("database.postgresdbPwd");

    /* *********************************************************
     * pg_dump:命令执行备份
     * -c:输出在创建数据库创建命令之前先清理(删除)该数据库对象的命令。
     * -d:将数据输出为的INSERT命令(而不是 COPY)。(PostgreSQL8.4上无效,暂时不添加)
     * -h:主机名
     * **********************************************************/
    String batLine501="pg_dump -c -h " + getText("database.postgresdbHost");

    //-U 数据库用户名
    String batLine502=" -U " + getText("database.postgresusername");

    //-p数据库端口
    String batLine503=" -p " + getText("database.postgresdbPort"); 

    //-E导出文件字符编码
    String batLine504=" -E " + getText("database.bakupcode");  //要备份的数据库名称
    String batLine505=" -D " + getText("database.postgresdbName") +" > ";
    batLine505=" " + getText("database.postgresdbName") +" > "; //取系统时间
    Date NowTimes = new Date(); //将时间格式化成yyMMddhhmmss(年月日时分秒)
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmssms");
    String BackupDbName = StringUtils.getPath(getText("database.bakupPath"))+ sdf.format(NowTimes) + ".backup";
    BackupDbName = getText("database.bakupPath")+ sdf.format(NowTimes) + ".backup";
    mBufWriter.write(batLine1);
    mBufWriter.newLine();
    mBufWriter.write(batLine2);
    mBufWriter.newLine();
    mBufWriter.write(batLine3);
    mBufWriter.newLine();
    mBufWriter.write(batLine4);
    mBufWriter.newLine();
    mBufWriter.write(batLine501);
    mBufWriter.write(batLine502);
    mBufWriter.write(batLine503);
    mBufWriter.write(batLine504);
    mBufWriter.write(batLine505);
    mBufWriter.write(BackupDbName);
    mBufWriter.newLine();
    mBufWriter.flush();
    mBufWriter.close();
    try {

    // 执行BackupDb.bat文件进行备份数据库
    Runtime.getRuntime().exec("BackupDb.bat"); 

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

             setErrMes("文件备份失败!");
           // 画面刷新处理
          return init();
    }
    // ==========================数据库备份代码 结束===========================//
      

  2.   

    写批处理文件时,windows下面一定要转到c盘的根目录才行。
    有时候可能是权限问题,需要密码,你直接写一句【set PGPASSWORD=xxxxxx】就自动执行可以跳过。这个是我做过项目的源码,现在项目已经部署了,一定好用,是struts2的框架下的。
    你可以试试,希望帮到你。