2013-06-28 165 views
0

我在java中編寫了一個簡單的附加程序,並將它加入了一個.exe文件。但是,當我嘗試甚至通過點擊它從我的桌面上運行的exe文件,我得到錯誤「的錯誤在啓動過程中已發生:」這個巨人的事情:嘗試運行.exefile時啓動時出現Java錯誤.exefile

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) 

我的清單文件包含:

「主類:ADDIT

它具有適當的兩行

的addit.java程序:

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

此外,順便說一句,即使運行cmd(當我運行addit.java)時,程序編譯並運行良好。

編輯:哦,等等,我很抱歉,事實證明addit.exe運行不正確。對不起,我一定很困惑。> <

+0

發佈您的清單文件內容 –

+0

必須在文件末尾有一個空行 –

+0

您可以發佈addit類嗎? – Jerome

回答

1

它對我來說工作正常。請確保您正在運行的罐子這樣

java -jar addit.jar 

輸入

1 2 

編輯:(嘗試這個具有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; 
+1

你爲什麼要傳遞參數? –

+0

輸入之間需要空格,因爲它是Scanner的默認分隔符。 @ user1569574您可以再試一次,讓我知道嗎? –

+0

當我在cmd的同一行輸入「1 2」時,它要求輸入第二個數字,但之後報告了總和。但沒關係,jar文件可以工作 – user1569574

0

的錯誤來自行15

at addit.main(addit.java:15) 

看看你的代碼上線15

x = scanner.nextInt(); 

此時你的程序試圖讀取從控制檯的整數值

Scanner scanner = new Scanner(System.in); 

scanner設置爲從讀取輸入System.in這是默認的操作系統的控制檯。但是,由於您的程序開箱即用,因此您的程序無法從讀取輸入中獲取。

重寫您的程序並將硬編碼的值改爲從用戶讀取。然後做所有這些事情。如果有效,你會得到你的答案。

相關問題