How to keep a java program running during error

Sorin Craciun

I am trying to make a java remote control progam, accesing it via a client, this is some code from the server. I am new to programming, and i would like some help. I want my server to keep running -> problem is, that when i assign a string value to CmdMenuDecision , the server crashes. Any way around it? I also get memory leak at ,, int CmdMenuDecision = new Scanner(System.in).nextInt(); ,, , i use @SuppressWarnings("resource") and i'm not sure if it's good or not.

private static Scanner SCANNER = new Scanner(System. in );
private String command1;
private String command2;
private String command3;
private String command4;

public void runtimeChoice() {
    System.out.println("what do you want to do? 1. Execute CMD Command");
    int CmdMenuDecision = new Scanner(System. in ).nextInt();
    switch (CmdMenuDecision) {
        case 1:
            CMDCommand();
            break;
        default:
            System.out.println("No valid answer");
            break;
    }
    private void CMDCommand() {
        System.out.println("CMD is working!");
        Runtime rt = Runtime.getRuntime();
        try {
            System.out.println("Insert desired command");
            command1 = CmdCommand.nextLine();
            command2 = CmdCommand.nextLine();
            command3 = CmdCommand.nextLine();
            command4 = CmdCommand.nextLine();
            rt.exec(new String[] {
                "cmd.exe", "/c", /*"start",*/
                command1, command2, command3, command4
            });
            System.out.println("Command: " + command1 + " " + command2 + " " + command3 + " " + command4 + " executed succesfully");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Mohammed Aouf Zouag

Change

int CmdMenuDecision = new Scanner(System.in).nextInt();

to

boolean flag = false;

do {
    try {
        int CmdMenuDecision = new Scanner(System.in).nextInt();
        flag = true; // If the execution flow reached this line then that means that the user input was correct; break the loop.
    }
    catch(InputMismatchException e) {
        System.out.println("Invalid input, try again.");
    }
}while(!flag);

This will make sure that you get an Integer input for the CmdMenuDecision variable.

Entering a String when the Scanner is expecting an int will throw an InputMismatchException; you need to handle it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Java program keep running, no compiler's error

From Dev

How to automatically close batch program, but keep java program running?

From Dev

Java ExecutorService - why does this program keep running?

From Dev

How to keep program running after MessageBox

From Dev

Running Java Program in another Program gives error

From Dev

Running Java Program in another Program gives error

From Dev

Java - How to keep running on exception

From Dev

Getting error during compiling java program

From Dev

Getting error during compiling java program

From Dev

Java heapsort program error during runtime

From Dev

Error: A JNI error has occurred, please check your installation and try again - during running Java program from Ubuntu terminal

From Dev

how to keep track of running location for a long running parallel program

From Dev

CMD keep running program

From Dev

Keep a program running in terminal

From Dev

How to keep batch file running while C# program quit?

From Dev

How to keep a Java program secure from hacking

From Dev

How to keep a Java program secure from hacking

From Dev

Error during "Running newaliases"

From Dev

Getting an error running this program as a command line argument in Java, how can I fix this?

From Java

How to get the path of running java program

From Dev

Java program keep infinite loop without any error message

From Dev

How to Stop a Running a Program Using Other Java Program

From Dev

Error running Go program

From Dev

Error in running program in hadoop?

From Dev

How to launch groovy console during runtime of a java program

From Dev

How to launch groovy console during runtime of a java program

From Dev

Error during running of scala codes

From Dev

Error during running thunderbird in Centos

From Dev

"Java Heap space Out Of Memory Error" while running a mapreduce program

Related Related

  1. 1

    Java program keep running, no compiler's error

  2. 2

    How to automatically close batch program, but keep java program running?

  3. 3

    Java ExecutorService - why does this program keep running?

  4. 4

    How to keep program running after MessageBox

  5. 5

    Running Java Program in another Program gives error

  6. 6

    Running Java Program in another Program gives error

  7. 7

    Java - How to keep running on exception

  8. 8

    Getting error during compiling java program

  9. 9

    Getting error during compiling java program

  10. 10

    Java heapsort program error during runtime

  11. 11

    Error: A JNI error has occurred, please check your installation and try again - during running Java program from Ubuntu terminal

  12. 12

    how to keep track of running location for a long running parallel program

  13. 13

    CMD keep running program

  14. 14

    Keep a program running in terminal

  15. 15

    How to keep batch file running while C# program quit?

  16. 16

    How to keep a Java program secure from hacking

  17. 17

    How to keep a Java program secure from hacking

  18. 18

    Error during "Running newaliases"

  19. 19

    Getting an error running this program as a command line argument in Java, how can I fix this?

  20. 20

    How to get the path of running java program

  21. 21

    Java program keep infinite loop without any error message

  22. 22

    How to Stop a Running a Program Using Other Java Program

  23. 23

    Error running Go program

  24. 24

    Error in running program in hadoop?

  25. 25

    How to launch groovy console during runtime of a java program

  26. 26

    How to launch groovy console during runtime of a java program

  27. 27

    Error during running of scala codes

  28. 28

    Error during running thunderbird in Centos

  29. 29

    "Java Heap space Out Of Memory Error" while running a mapreduce program

HotTag

Archive