Results 1 to 10 of 10
Thread: Java Exception Has Occured
- 08-29-2011, 02:05 PM #1
Member
- Join Date
- Aug 2011
- Posts
- 19
- Rep Power
- 0
Java Exception Has Occured
[I also posted this in the new to Java section, but the post has been buried and I thought this post would make more sense here]
Exported my code (see below) in Eclipse (right click top directory -> export -> jar file -> finish) and get the "A Java Exception Has Occured" error when I try to open the program that has just been exported.
I'm opening the generated .jar file by double clicking on it (I assume its opening with Java or something related as has the Java logo and is called a java execuatable file).
The only contents of Mainfest.mf is "Maifest-Version: 1.0" (after extracting with WinRar and opening in Notepad++).
Using command prompt to open the MainClass main method makes the program run (java -cp Done.jar;. MainClass).
Running in command prompt (without opening MainClass myself):
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\Eden>cd Desktop/Java
C:\Users\Eden\Desktop\Java>java -jar Done.jar
Exception in thread "main" java.lang.NullPointerException
at sun.launcher.LauncherHelper.getMainClassFromJar(Un known Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unkno wn Source)
C:\Users\Eden\Desktop\Java>
In the other post we determined that Eclipse was not going to MainClass - can someone help me on this?Java Code:/** * Guess Your OS! * @author Eden Crow * */ import java.io.*; public class MainClass{ public static void main(String[] args) throws IOException { String input; String OS; System.out.println("Welcome to Guess Your OS! - Read to play?"); InputStreamReader isrPlayGame = new InputStreamReader(System.in); BufferedReader brPlayGame = new BufferedReader(isrPlayGame); input = brPlayGame.readLine(); input = input.replaceAll("\\\\n", ""); if(input.equalsIgnoreCase("Yes")){ /* */ OS = System.getProperty("os.name"); InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); System.out.println("Your OS has been calcualted - now have a guess what it is!"); input = br.readLine(); input = input.replaceAll("\\\\n", ""); if(input.equalsIgnoreCase(OS)){System.out.println("Thank god - you know what your operating system is (" + OS + ")"); /* */ if(OS == "Windows Vista"){System.out.println("What a second... you have Windows Vista? (Laughs in your face)");}} else if(!input.equals(OS)){System.out.println("Wrong! - You thought your OS is " + input + " but is actually " + OS + "!");} else{System.out.println("Error");}; try {Thread.sleep(1000);} catch(InterruptedException e){}; System.out.println("Thanks for playing Guess Your OS! Press enter to close the game..."); InputStreamReader isrEndGame = new InputStreamReader(System.in); BufferedReader brEndGame = new BufferedReader(isrEndGame); input = brEndGame.readLine(); System.exit(0);} else {System.exit(0);}; } }
Thanks,
EdenC
[Original Post Link]
- 08-29-2011, 04:19 PM #2
Open the jar file in a zip utility and look at the manifest file. Copy and post its contents here.
The Main-Class: entry should look like this: Main-Class: MainClass
if the jar file is to start execution with your program.
Looking at the error message, it seems that your IDE has added its own code to the jar file.
- 08-29-2011, 04:29 PM #3
When you export a jar a dialog screen name JAR Manifest Specification appear. Here choose "Use existing manifest from workspace" and click browse and then navigate to your manifest file, finish and call your jar. I could run your code successfully from a jar file.
- 08-29-2011, 04:36 PM #4
Member
- Join Date
- Aug 2011
- Posts
- 19
- Rep Power
- 0
Thanks for all help so far!
----------------------------------------------------------------------------------------------------------------
I already have in the OP:
----------------------------------------------------------------------------------------------------------------The only contents of Mainfest.mf is "Maifest-Version: 1.0" (after extracting with WinRar and opening in Notepad++).
This seems to make sense - I had selected "Finish" before getting to this stage as assumed options futher down were optional. However, I am a little confused at this part as when I select to use one from a workspace the two files I can choose from are ".classpath" and ".project" - which one should I select?
----------------------------------------------------------------------------------------------------------------
Thanks,
EdenC
- 08-29-2011, 04:42 PM #5
That is not enough. You need the Main-Class: entry.The only contents of Mainfest.mf is "Maifest-Version: 1.0"
- 08-29-2011, 04:43 PM #6
Member
- Join Date
- Aug 2011
- Posts
- 19
- Rep Power
- 0
- 08-29-2011, 04:46 PM #7
none. set your classpath in your system environment starting or ending with a dot. so in a console the actual directory will always be encluded in your classpath.
about the mentioned dialog box: you can also let eclipse generate the manifest-file for you, but then you must tell eclipse which class contains the main() method in "Select the class of the application entry point" by pressing the Browse button.Last edited by j2me64; 08-30-2011 at 09:06 AM.
- 08-30-2011, 10:29 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Classpath must be included in the Manifest if you have extra jar files to allow the app to run. You should never set the system environment classpath. Each classpath should be app specific, and therefore set when you are executing.
In any case, in the case of executable jar files any classpath outside of the manifest tends to be ignored.
I'd do it this way. Let Eclipse do the work (though I would read up on manifests just so you know what it is it's actually doing).
- 08-30-2011, 11:18 AM #9
Member
- Join Date
- Aug 2011
- Posts
- 19
- Rep Power
- 0
Thanks for everyone's help so far!
I have followed through the exporting process and still can't get the program to run. Here is what I do/select:
File -> Export -> Java -> Jar File -> Next -> Click on "MainProj" folder and keep other choices the same (export generated class files and resources, compress the contents of the JAR file and add directory entries) -> Next -> Keep all these the same (Select options for handling problems: export class files with compile errors, export class files with complile warnings, build projects if not build automatically) -> Next -> Generate the mainifest file, seal some packages (nothing sealed), MainClass: MainClass -> Finish
Double clicking does nothing. I'll do another post with command prompt information like in OP.
Thanks,
EdenC
- 08-30-2011, 04:40 PM #10
Similar Threads
-
Java Exception Has Occured
By EdenC in forum New To JavaReplies: 12Last Post: 08-29-2011, 04:20 PM -
unreported exception java.lang.Exception; must be caught or declared to be thrown
By arc_remiel in forum New To JavaReplies: 5Last Post: 02-14-2011, 11:39 PM -
After installed Tomcat 404 error occured
By srinivasmallabathula in forum New To JavaReplies: 2Last Post: 01-31-2011, 11:04 AM -
Please solve my error occured java.lang.NullPointerException
By Viruthagiri in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 03-24-2010, 12:08 PM -
Error occured during initialization of VM
By red in forum New To JavaReplies: 4Last Post: 10-02-2008, 07:08 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks