Home > Mobile >  Linux terminal command execution with java
Linux terminal command execution with java

Time:08-08

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.

    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.

Can someone help me? Thank you very much, waiting for a response

News for "Some Programmers Dude" This is the code now

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

CodePudding user response:

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.

I recommend you create a new function to run a command and wait for it to finish:

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.

  • Related