简体   繁体   English

Linux上的Java执行进程

[英]Java execute process on linux

I've been struggling for a while now with this problem and i can't seem to fix it. 我一直在努力解决这个问题,我似乎无法修复它。 i already have tried different approaches (Runtime.exec(), ProcessBuiler) but none seem to work. 我已经尝试过不同的方法(Runtime.exec(),ProcessBuiler),但似乎都没有。

This is my issue. 这是我的问题。 I have a laptop which is always on. 我有一台永远在线的笔记本电脑。 This laptop runs a java tool connected to an arduino via usb to turn on and off the lights in the house. 这台笔记本电脑运行一个java工具连接到arduino通过USB打开和关闭房子里的灯。 i have created this program myself, therefore i'm also doing some regular maintenance work on it. 我自己创建了这个程序,因此我也在做一些定期的维护工作。 Recently i have added a button to restart the program from my html interface (in case i have an update, or if for some other reason i might need to restart the program or i decide to implement auto updating in the near future). 最近我添加了一个按钮,从我的html界面重启程序(如果我有更新,或者由于某些其他原因我可能需要重新启动程序或我决定在不久的将来实现自动更新)。

This idea behind this is to start a second instance of the application from the first instance and then System.exit(0) the first instance. 这背后的想法是从第一个实例启动应用程序的第二个实例,然后从System.exit(0)启动第一个实例。

For some reason i'm not able to start a second instance of the application. 由于某种原因,我无法启动应用程序的第二个实例。 Here's some code. 这是一些代码。

public void shutdown(boolean restart) {
        if (this.serial != null) {
            this.serial.disconnect();
        }

        if (restart) {
            System.out.println(this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath());
            String startupCommand = "java -jar \"" + this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath().replace("%20", " ") + "\"";
            ProcessBuilder builder = new ProcessBuilder();

//            String[] command = new String[1];
//            command[0] = "-jar \"" + (System.getProperty("user.dir") + "/Home_Automation_Executor.jar") + "\"";
            try {
//                //System.out.println("Restarting Home Automation with command: " + command[0]);
//                System.out.println("Restarting Home Automation with command: " + startupCommand);
//                Runtime.getRuntime().exec("bash");
//                Process proc = Runtime.getRuntime().exec(startupCommand);
                Process proc = builder.command(startupCommand).start();
                InputStream stderr = proc.getErrorStream();
                InputStreamReader isr = new InputStreamReader(stderr);
                BufferedReader br = new BufferedReader(isr);
                String line = null;
                System.out.println("<ERROR>");
                while ((line = br.readLine()) != null) {
                    System.out.println(line);
                }
                System.out.println("</ERROR>");
                int exitVal = 0;
                try {
                    exitVal = proc.waitFor();
                } catch (InterruptedException ex) {
                    Logger.getLogger(Engine.class.getName()).log(Level.SEVERE, null, ex);
                }
                System.out.println("Process exitValue: " + exitVal);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        System.out.println("Terminating Home Automation");
        System.exit(0);
    }

java.io.IOException: Cannot run program "java -jar "/Users/NightWalker/Dropbox/Development/Source Code/Java/NightWare Tools/Home Automation/Home Automation Executor/dist/Home_Automation_Executor.jar"": error=2, No such file or directory at java.lang.ProcessBuilder.start(ProcessBuilder.java:460) at home.automation.executor.Engine.shutdown(Engine.java:186) at home.automation.executor.webserver.HTTPGenerator._handleActionCommand(HTTPGenerator.java:190) at home.automation.executor.webserver.HTTPGenerator._generateHTTPPage(HTTPGenerator.java:165) at home.automation.executor.webserver.HTTPGenerator.getHTTPPage(HTTPGenerator.java:58) at home.automation.executor.webserver.HTTPRequestHandler.run(HTTPRequestHandler.java:160) Caused by: java.io.IOException: error=2, No such file or directory at java.lang.UNIXProcess.forkAndExec(Native Method) at java.lang.UNIXProcess.(UNIXProcess.java:53) at java.lang.ProcessImpl.start(ProcessImpl.java:91) at java.lang.ProcessBuilder.start(ProcessBuilder.java:453 java.io.IOException:无法运行程序“java -jar”/ Users / NightWalker / Dropbox / Development / Source Code / Java / NightWare Tools / Home Automation / Home Automation Executor / dist / Home_Automation_Executor.jar“”:error = 2,家里的home.automation.executor.Engine.shutdown(Engine.java:186)java.lang.ProcessBuilder.start(ProcessBuilder.java:460)上没有这样的文件或目录.automation.executor.webserver.HTTPGenerator._handleActionCommand( HTTP.Georator.java:190)home.automation.executor.webserver.HTTPGenerator._generateHTTPPage(HTTPGenerator.java:165)at home.automation.executor.webserver.HTTPGenerator.getHTTPPage(HTTPGenerator.java:58)at home.automation.executor .webserver.HTTPRequestHandler.run(HTTPRequestHandler.java:160)引起:java.io.IOException:error = 2,java.lang.UNIXProcess中java.lang.UNIXProcess.forkAndExec(Native Method)中没有此类文件或目录。 (UNIXProcess.java:53)java.lang.ProcessImpl.start(ProcessImpl.java:91)java.lang.ProcessBuilder.start(ProcessBuilder.java:453) ) ... 5 more )...还有5个

The problem is this: 问题是这样的:

String startupCommand = "java -jar \"" + this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath().replace("%20", " ") + "\"";

/* more stuff */ builder.command(startupCommand);

This means Jav will look for a command named java -jar ...stuff with spaces... . 这意味着Jav将寻找一个名为 java -jar ...stuff with spaces...的命令java -jar ...stuff with spaces... But what you want is, that Java looks for a command named java and give that command several parameters. 但你想要的是,Java寻找一个名为java的命令,并为该命令提供几个参数。

You should use 你应该用

/*...*/ builder.command("java", "-jar", jarLocation) /*...*/

Since it is another Java program you might want to consider running it in the same process because it's much easier to communicate between the two programs if they live in the same process. 由于它是另一个Java程序,您可能需要考虑在同一个进程中运行它,因为如果它们位于同一个进程中,则在两个程序之间进行通信要容易得多。 Have you tried running the command outside your program? 您是否尝试在程序外运行命令? Does it work? 它有用吗? What does the meta-inf.mf file in the jar hold? jar中的meta-inf.mf文件包含什么? It might be that the classpath in the meta-inf.mf file isn't relative so any dependent jars can't be found. 可能是meta-inf.mf文件中的类路径不是相对的,因此无法找到任何相关的jar。

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

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