user1569574
user1569574

Reputation: 21

Java error on startup when try to run .exefile

I wrote a simple addition program in java and have made it into a .exe file. But when I try to run the exe file, even by clicking on it from my desktop, I get the error "An error has occured during startup:" with this giant thing:

java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at addit.main(addit.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.exe4j.runtime.LauncherEngine.launch(Unknown Source)
at com.exe4j.runtime.WinLauncher.main(Unknown Source)

My manifest files contains:

"Main-Class: addit

"

It has the proper two lines

The addit.java program:

import java.util.Scanner;

public class addit
{
  public static void main (String [] args)
  {
    int x;
    int y;
    int z;

System.out.println("Welcome to Addit!");

System.out.println("Please enter the first digit.");
Scanner scanner = new Scanner(System.in);
x = scanner.nextInt();

System.out.println("Please enter the second digit.");
y = scanner.nextInt();

z = x + y;

System.out.println("The sum of " + x + " and " + y + " is " + z);
  }
}

Also, just by the way, the program compiles and runs fine, even when run through the cmd (when I run addit.java).

EDIT: Oh, wait, I'm sorry, it turns out the addit.exe isn't running correctly. I'm sorry, I must have been confusing.. ><

Upvotes: 0

Views: 429

Answers (2)

Mohammad Faisal
Mohammad Faisal

Reputation: 5959

The error is coming from line 15

at addit.main(addit.java:15)

Look at your code on line 15

x = scanner.nextInt();

At this point your program trying to read an integer value from console as

Scanner scanner = new Scanner(System.in);

scanner is set to read the input from the System.in which is by default the console of the OS. But, as your are running your program out of the box so your program not getting where from read the input.

Rewrite your program and put hardcoded values instead reading from user. Then do all this stuff. If works, you will get your answer.

Upvotes: 0

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51721

It works fine for me. Make sure you're running the jar this way

java -jar addit.jar

Input

1 2

EDIT : (try this with addit.exe)

Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the first digit: ");
x = scanner.nextInt();

scanner.nextLine(); // skips '\n' causing the problem

System.out.println("Please enter the second digit: ");
y = scanner.nextInt();

z = x + y;

Upvotes: 1

Related Questions