本文介绍了Linux上的Java执行进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力解决这个问题,我似乎无法修复它。
我已经尝试了不同的方法(Runtime.exec(),ProcessBuiler)但似乎都没有。

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.

这是我的问题。
我有一台笔记本电脑一直打开。这台笔记本电脑运行一个java工具连接到arduino通过USB打开和关闭房子里的灯。我自己创建了这个程序,因此我也在做一些定期的维护工作。最近我添加了一个按钮,从我的html界面重启程序(如果我有更新,或者由于某些其他原因我可能需要重新启动程序或我决定在不久的将来实现自动更新)。

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. 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).

这背后的想法是从第一个实例启动应用程序的第二个实例,然后从System.exit(0)启动第一个实例。

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.

由于某种原因,我无法启动应用程序的第二个实例。
这是一些代码。

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);
    }




推荐答案

问题是:

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

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

这意味着Jav会查找命令名为 java -jar ...包含空格的东西...... 。但你想要的是,Java寻找一个名为 java 的命令,并为该命令提供几个参数。

This means Jav will look for a command named java -jar ...stuff with spaces.... But what you want is, that Java looks for a command named java and give that command several parameters.

你应该使用

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

这篇关于Linux上的Java执行进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 11:19
查看更多