我正在尝试从Java调用pg_dump。这是我要执行的命令。但是--file参数中指定的文件未创建。
/usr/local/Cellar/postgresql@9.6/9.6.10_1/bin/pg_dump --format=t --verbose --file=/Users/abc/Downloads/backups/backup-1549946580007.backup --no-password
这是我用来执行相同代码的代码。我正在使用Apache commons exec执行命令。我还尝试过ProcessBuilder和Process来执行相同的操作。
cmd = "sh";
cmdArg = "-c";
pgCmdArg = System.getenv("POSTGRES_BIN") + "pg_dump";
pgVerboseArg = "--verbose";
fileArg = "--file=" + inputFile;
noPassWordArg = "--no-password";
Map<String, String> env=new HashMap<>();
env.put("PGHOST", "localhost");
env.put("PGUSER", dbUserName);
env.put("PGPASSWORD", dbPass);
env.put("PGPORT", "5432");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
CommandLine commandline = CommandLine.parse(cmd).addArgument(cmdArg).addArgument(pgCmdArg)
.addArgument("--format=t").addArgument(pgVerboseArg).addArgument(fileArg)
.addArgument(noPassWordArg).addArgument(dbName);
DefaultExecutor exec = new DefaultExecutor();
PumpStreamHandler streamHandler = new
PumpStreamHandler(outputStream);
exec.setStreamHandler(streamHandler);
int exitCode = exec.execute(commandline, env);
知道为什么不创建文件吗?