简体   繁体   English

Linux 终端命令执行与 java

[英]Linux terminal command execution with java

i need help with my program.我的程序需要帮助。 what I have to do is that when I click a button my java program will have to execute commands on the linux terminal as shown below, and after that it will have to shut down the program.我要做的是,当我单击一个按钮时,我的 java 程序将必须在 linux 终端上执行命令,如下所示,然后必须关闭程序。

    String com1 = "rm -r /home/ctm-tech/Documenti/App/Thermaskin";
    String com2 = "cp -r /media/ctm-tech/FXPoWer_1/Thermaskin /home/ctm-tech/Documenti/App/";
    String com3 = "java -jar /home/ctm-tech/Documenti/App/Thermaskin/dist/Thermaskin.jar";
    String com4 = "sudo eject /media/ctm-tech/FXPoWer_1";
    
    try {
        Process p1 = Runtime.getRuntime().exec(com1);
        Process p2 = Runtime.getRuntime().exec(com2);
        Process p3 = Runtime.getRuntime().exec(com3);
        Process p4 = Runtime.getRuntime().exec(com4);
    } catch (IOException e) {
        System.out.println("Qualcosa è andato storto"  + e.toString());
    }

    try {
        Thread.sleep(6000);
    } catch (InterruptedException ex) {}

    System.exit(0);

The problem is that my program only executes the first command, and sometimes it doesn't execute anything.问题是我的程序只执行第一个命令,有时它什么也不执行。

The commands must update the running program, with a later version present inside the FXPoWer_1 pendrive.这些命令必须更新正在运行的程序,并在 FXPoWer_1 pendrive 中提供更高版本。

Can someone help me?有人能帮我吗? Thank you very much, waiting for a response非常感谢,等待回复

News for "Some Programmers Dude" This is the code now “Some Programmers Dude”的新闻 这是现在的代码

private void jButton_aggiornaActionPerformed(java.awt.event.ActionEvent evt) {                                                 
    runCommand("cp -r /media/ctm-tech/FXPoWer_1/Thermaskin1 /home/ctm-tech/Documenti/App/");
    runCommand("java -jar /home/ctm-tech/Documenti/App/Thermaskin1/dist/Thermaskin.jar");
    runCommand("rm -r /home/ctm-tech/Documenti/App/Thermaskin");
    runCommand("mv /home/ctm-tech/Documenti/App/Thermaskin1 /home/ctm-tech/Documenti/App/Thermaskin");
    runCommand("sudo eject /media/ctm-tech/FXPoWer_1");        

    System.exit(0);
}         

The commands you try to run will all run in parallel, without any control over which will run first or their order.您尝试运行的命令将全部并行运行,而无法控制先运行的命令或它们的顺序。

That means some command can (and will) fail if the are happening in the wrong order.这意味着如果以错误的顺序发生,某些命令可能(并且将会)失败。

One simple way to solve it is to call waitFor on the Process object, to wait for each command to finish before starting the next.解决它的一种简单方法是在Process object 上调用waitFor ,等待每个命令完成后再开始下一个命令。

I recommend you create a new function to run a command and wait for it to finish:我建议您创建一个新的 function 来运行命令并等待它完成:

private void runCommand(String command) {
    try {
        Process proc = Runtime.getRuntime().exec(command);

        // Wait for command to finish
        proc.waitFor();
    } catch (IOException e) {
        System.out.println("Error running command: "  + e.toString());
    }
}

Then you can call this for each of your commands:然后你可以为你的每个命令调用它:

runCommand("rm -r /home/ctm-tech/Documenti/App/Thermaskin");
runCommand("cp -r /media/ctm-tech/FXPoWer_1/Thermaskin /home/ctm-tech/Documenti/App/");
runCommand("java -jar /home/ctm-tech/Documenti/App/Thermaskin/dist/Thermaskin.jar");
runCommand("sudo eject /media/ctm-tech/FXPoWer_1");

These commands will now run in the exact sequence called.这些命令现在将按照调用的确切顺序运行。

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

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