Linux terminal command execution with java

FXPoWer_

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);
}         
Some programmer dude

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

terminal command execution on startup

From Dev

Executing a Linux command in Terminal from Java

From Dev

Terminal command for infinite execution of application

From Dev

How to serialize command execution on linux?

From Dev

Linux command execution rate limiting

From Dev

java command not found in terminal

From Dev

javac and java command in terminal

From Java

Open Linux terminal command in PHP

From Java

Is there a command in java to measure the execution time?

From Java

Java stream operation order of execution at terminal point

From Dev

java thread stops execution when clicking in terminal

From Dev

Defunct processes when java start terminal execution

From Dev

Java execute debian terminal command with "|"

From Dev

Is there any linux command to speed up the execution of the files?

From Dev

paramiko python script for linux command execution

From Dev

How to handle tabulated terminal output after NSTask command execution with Swift

From Dev

How to automatically switch to terminal when it finished its command's execution?

From Dev

Python PyQt5 Terminal Command execution problem on a button click

From Dev

How to get the terminal text those are overflowed during command execution?

From Dev

Set execution time for Script command to record terminal activity

From Dev

Wait till NSTask completes its execution of terminal command

From Dev

How to add backspace in a Linux terminal command?

From Dev

linux bash terminal function not closing on command

From Dev

Linux reboot the terminal for npm command function

From Java

Run any Linux terminal command from TypeScript?

From Java

C# execute a terminal command in linux

From Java

How to use vi to edit a command in terminal on Linux?

From Dev

ctrl + r in linux / ubuntu terminal command line

From Java

How to loop an executable command in the terminal in Linux?

Related Related

  1. 1

    terminal command execution on startup

  2. 2

    Executing a Linux command in Terminal from Java

  3. 3

    Terminal command for infinite execution of application

  4. 4

    How to serialize command execution on linux?

  5. 5

    Linux command execution rate limiting

  6. 6

    java command not found in terminal

  7. 7

    javac and java command in terminal

  8. 8

    Open Linux terminal command in PHP

  9. 9

    Is there a command in java to measure the execution time?

  10. 10

    Java stream operation order of execution at terminal point

  11. 11

    java thread stops execution when clicking in terminal

  12. 12

    Defunct processes when java start terminal execution

  13. 13

    Java execute debian terminal command with "|"

  14. 14

    Is there any linux command to speed up the execution of the files?

  15. 15

    paramiko python script for linux command execution

  16. 16

    How to handle tabulated terminal output after NSTask command execution with Swift

  17. 17

    How to automatically switch to terminal when it finished its command's execution?

  18. 18

    Python PyQt5 Terminal Command execution problem on a button click

  19. 19

    How to get the terminal text those are overflowed during command execution?

  20. 20

    Set execution time for Script command to record terminal activity

  21. 21

    Wait till NSTask completes its execution of terminal command

  22. 22

    How to add backspace in a Linux terminal command?

  23. 23

    linux bash terminal function not closing on command

  24. 24

    Linux reboot the terminal for npm command function

  25. 25

    Run any Linux terminal command from TypeScript?

  26. 26

    C# execute a terminal command in linux

  27. 27

    How to use vi to edit a command in terminal on Linux?

  28. 28

    ctrl + r in linux / ubuntu terminal command line

  29. 29

    How to loop an executable command in the terminal in Linux?

HotTag

Archive