0

In Netbeans, my program works perfectly. It gets user input using Scanner.

However, when I run the jar file in command prompt, it skips all the user input and runs the program anyways. Why does it do this and how do I correct this?

Example:

public static void main(String[] args) 
{
System.out.print("How many teachers need an assignment (two classes will be assigned to each teacher)? ");
        numTeachers = input.next();
}

It never even prints out the first line either

Jesse Martinez
  • 403
  • 2
  • 5
  • 8
  • Without code we can only guess at one of many issues that might cause this. For more help post a Minimal, Complete, and Verifiable example: http://stackoverflow.com/help/mcve – sorifiend Jun 02 '16 at 04:41
  • 1
    Ok looks like you are starting your jar file incorrectly. How do you run your program? Do you first open a console, and then launch your jar from inside the console? – sorifiend Jun 02 '16 at 04:53

1 Answers1

0

Because that first line never prints I think you will find your issue is caused by how you run your program, because if it works in netbeans that that's the only difference.

First open a console window in the folder with your program then run the jar file like normal:

java -jar myapplication.jar

If you simply double click your jar file then it will run instantly, and because there is no console it will close instantly. Java programs that use the console must also be run from the console/command prompt/terminal.

Edit: Just in case, it is worth double checking how you setup your scanner. It should look something like this example: https://stackoverflow.com/a/17691245/1270000

     public static void main(String args[]){

            Scanner scanner = new Scanner(System.in);
            int eid,sid;
            String ename;
            System.out.println("Enter Employeeid:");
            eid=scanner.nextInt();
            scanner.nextLine(); //This is needed to pick up the new line
            System.out.println("Enter EmployeeName:");
            ename=scanner.nextLine();
            System.out.println("Enter SupervisiorId:");
            sid=(scanner.nextInt());
    }
Community
  • 1
  • 1
sorifiend
  • 5,927
  • 1
  • 28
  • 45