简体   繁体   English

无法执行命令:无此类文件或目录/ Java

[英]Can't execute a command: no such file or directory / Java

I am trying to run a .jar file via another program. 我正在尝试通过另一个程序运行.jar文件。 For example, I have a HelloWorld.jar file which opens a dialog saying "Hello World". 例如,我有一个HelloWorld.jar文件,它将打开一个对话框,显示“ Hello World”。 And I have a Test.jar program. 我有一个Test.jar程序。 When I do something in the Test.jar (ie click some button), it should run the HelloWorld.jar . 当我在Test.jar执行某项操作(即单击某些按钮)时,它应运行HelloWorld.jar

The way I am currently doing so is running a terminal command java -jar HelloWorld.jar using ProcessBuilder . 我目前这样做的方式是使用ProcessBuilder运行终端命令java -jar HelloWorld.jar However, I get this error: 但是,我收到此错误:

Debug: "C:\Users\Asus\.fairplay\data\apps\Amnesia\.tmp524\.bin" exists: true
java.io.IOException: Cannot run program "java -jar Amnesia.jar" (in directory "C:\Users\Asus\.fairplay\data\apps\Amnesia\.tmp524\.bin"): CreateProcess error=2, No such file or directory
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
    at me.darksidecode.fairplay.client.util.Utils.execute(Utils.java:74)
    at me.darksidecode.fairplay.client.app.AppLauncher.launch0(AppLauncher.java:61)
    at me.darksidecode.fairplay.client.app.AppLauncher.launch(AppLauncher.java:37)
    at me.darksidecode.fairplay.client.app.AppLoader.downloadAndRun(AppLoader.java:28)
    at me.darksidecode.fairplay.client.gui.frame.GuiLauncher.onPacketReceiving(GuiLauncher.java:165)
    at me.darksidecode.fairplay.client.core.FairPlayClient.lambda$processPacket$1(FairPlayClient.java:120)
    at java.lang.Thread.run(Thread.java:745)
    Caused by: java.io.IOException: CreateProcess error=2, No such file or directory
    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)
    ... 7 more

I am 100% sure that both the file and the directory exist , and I even checked that. 100%确定文件和目录都存在 ,甚至检查了一下。 As you can see in the error over here, there is also a debug message proving that. 如您在此处的错误中所见,还有一条调试消息证明了这一点。

My code for command execution: 我的命令执行代码:

public static Process execute(final String path, final String cmd, final boolean removeJavaOptions) {
    File f = Files.getFile(path);
    System.out.println("Debug: \"" + f.getAbsolutePath() + "\" exists: " + f.exists());

    try {
        final ProcessBuilder pb = new ProcessBuilder(cmd);

        pb.directory(Files.getFile(path));
        pb.redirectErrorStream(true);

        if (removeJavaOptions)
            pb.environment().remove("_JAVA_OPTIONS");
        return pb.start();
    } catch (final Exception ex) {
        ex.printStackTrace();
        return null;
    }
}

The usage of this method itself: 此方法本身的用法:

Utils.execute(bin.getAbsolutePath().replace(bin.getName(), ""), "java -jar " + bin.getName(), false);

I have not found any useful answers to this question on StackOverflow or anywhere else yet. 我在StackOverflow或其他任何地方都找不到针对此问题的有用答案。 Hopefully there is a solution to solve this. 希望有解决此问题的解决方案。

The ProcessBuilder constructor accepts either a List<String> , or a varargs (several strings), eg ProcessBuilder构造函数接受List<String>或varargs(多个字符串),例如

new ProcessBuilder( "command", "arg1", "arg2", "arg3" );

Instead of passing it the commands and the arguments separately, you have tried to pass it the command together with the arguments in the same string. 您没有尝试将命令和参数分别传递给它,而是尝试将命令和参数一起传递给它在同一字符串中。

The builder interprets the first argument as the name of the command. 构建器将第一个参数解释为命令的名称。 Thus, it believes that you are trying to run a file called java -jar Amnesia.jar . 因此,它认为您正在尝试运行名为java -jar Amnesia.jar的文件。 It thinks you simply have an executable file with spaces in its name. 它认为您只是有一个可执行文件,其名称中带有空格。 But of course, the operating system can't find such an executable file. 但是,当然,操作系统找不到这样的可执行文件。

What you should do is run the command with the arguments separated from the command, that is: 您应该执行的操作是使用与命令分开的参数运行命令,即:

new ProcessBuilder( "java", "-jar", "Amnesia.jar" );

So you'll need to reconstruct your execute method, such that either the cmd parameter is a List<String> , an array ( String[] ), or it is the last parameter and is a varargs parameter. 因此,您需要重新构造execute方法,以便cmd参数是List<String> ,数组( String[] ),或者它是最后一个参数并且是varargs参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM