通过java代码执行命令时出错

时间:2016-08-10 09:40:32

标签: java command

我正在尝试通过java代码安装postgreSql.exe。我将安装程序exe文件保存在workspace/project-root-folder/data下。我试图通过我的java代码执行下面的命令:

installationCommand = "postgresql-9.5.3-1-windows-x64.exe --mode unattended --installer-language en --serverport 5433 --superaccount test_admin --superpassword  ‘password’"

以下是我在java代码中使用的代码片段:

System.out.println("Lets install postgreSql");
ProcessBuilder b = new ProcessBuilder();
b.directory(new File("data").getAbsoluteFile());
b.command(installationCommand);
b.start();

通过此设置,我得到异常

   java.io.IOException: Cannot run program "postgresql-9.5.3-1-windows-x64.exe --mode unattended --installer-language en --serverport 5433 --superaccount protoel_admin --superpassword  ‘password’" (in directory "C:\DurgeshProjectWork\Workspace\protoel-core\data"): CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
    at frontend.guifx.pginstallation.PgStandaloneInstaller.installPg(PgStandaloneInstaller.java:27)
    at frontend.guifx.controller.ProtoelController.initialize(ProtoelController.java:386)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2445)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3218)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3179)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3152)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3128)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3108)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3101)
    at frontend.guifx.main.ProToelApplication.start(ProToelApplication.java:34)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863)
    at com.sun.javafx.application.LauncherImpl$$Lambda$53/953109155.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl$$Lambda$45/2034688500.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
    at com.sun.javafx.application.PlatformImpl$$Lambda$48/3243045.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/1007251739.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
    at com.sun.glass.ui.win.WinApplication$$Lambda$36/1508395126.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(ProcessImpl.java:386)
    at java.lang.ProcessImpl.start(ProcessImpl.java:137)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
    ... 25 more

请有人告诉我如何通过java代码从特定目录执行命令。请帮忙。提前致谢

2 个答案:

答案 0 :(得分:1)

您似乎在.directory()方法中指定了错误的目录。你必须仔细检查。确保传递给此方法的值指向postgresql安装程序目录。

答案 1 :(得分:0)

要使用此命令,必须将每个参数拆分为一个字符串数组。请参阅ProcessBuilder.command(String... args)文档。您可以使用installationCommand.split(" ")获取String[]

还要检查目录路径是否正确,以防万一。

相关问题