Results 1 to 13 of 13
  1. #1
    EdenC is offline Member
    Join Date
    Aug 2011
    Posts
    19
    Rep Power
    0

    Default Java Exception Has Occured

    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. Help please!

    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

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default

    when I try to open the program
    Please explain what file you are opening and what program you are using to open that file.

  3. #3
    EdenC is offline Member
    Join Date
    Aug 2011
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Please explain what file you are opening and what program you are using to open that file.
    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).
    Should I be opening it with something?

    Thanks,

    EdenC

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default

    The double click should work if there are no errors.
    To see if there are any error messages, you need to use a command prompt window. Open a command prompt window, change directory to the directory with the jar file (cd command) and enter this command:
    java -jar <YOURJARFILENAME>.jar

    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

  5. #5
    EdenC is offline Member
    Join Date
    Aug 2011
    Posts
    19
    Rep Power
    0

    Default

    As requested:
    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>
    Thanks,
    EdenC

    EDIT: The word "unknown" seems to mess up in the quote but is just "(Unknown Source)"

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default

    I have no idea what your IDE has put into the Done.jar. I don't know of The classes listed in the error message.
    Can you open the jar in a zip file utility and look at the manifest file. It should have a line starting with:
    Main-Class:
    what is the full contents of that line?
    The is where the name of your class file should be:
    Main-Class: MainClass

    Also try this. Open a command prompt and change to the directory with the jar file and enter this command:
    java -cp Done.jar;. MainClass

    It will call the MainClass's main method and bypass the stuff the IDE put into the jar.

  7. #7
    EdenC is offline Member
    Join Date
    Aug 2011
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    I have no idea what your IDE has put into the Done.jar. I don't know of The classes listed in the error message.
    Can you open the jar in a zip file utility and look at the manifest file. It should have a line starting with:
    Main-Class:
    what is the full contents of that line?
    The is where the name of your class file should be:
    Main-Class: MainClass

    Also try this. Open a command prompt and change to the directory with the jar file and enter this command:
    java -cp Done.jar;. MainClass


    It will call the MainClass's main method and bypass the stuff the IDE put into the jar.
    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.

    Thanks,

    EdenC

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,925
    Rep Power
    19

    Default

    You need to tell Eclipse what the entry point for the program is (ie the main class) so it can create the Manifest correctly.
    I can't remember how to do that, though.

  9. #9
    EdenC is offline Member
    Join Date
    Aug 2011
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    You need to tell Eclipse what the entry point for the program is (ie the main class) so it can create the Manifest correctly.
    I can't remember how to do that, though.
    Shouldn't Eclipse have done this when the jar file was created?

    Thanks,

    EdenC

  10. #10
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,925
    Rep Power
    19

    Default

    Not necessairly, since your project may not be intended to provide an executable jar, but just a library.
    Netbeans goes the other way and defaults to each one being executable.

  11. #11
    EdenC is offline Member
    Join Date
    Aug 2011
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Not necessairly, since your project may not be intended to provide an executable jar, but just a library.
    Netbeans goes the other way and defaults to each one being executable.
    So how would I get Eclipse to make it execuatable during the compiling stage?

  12. #12
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,925
    Rep Power
    19

    Default

    As I said, no idea.
    I haven't got Eclipse in front of me, and I never bother remembering these things...I usually look them up in the manual.

  13. #13
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    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.

Similar Threads

  1. unreported exception java.lang.Exception
    By Marisabel in forum New To Java
    Replies: 6
    Last Post: 05-01-2011, 03:21 AM
  2. Replies: 5
    Last Post: 02-14-2011, 11:39 PM
  3. After installed Tomcat 404 error occured
    By srinivasmallabathula in forum New To Java
    Replies: 2
    Last Post: 01-31-2011, 11:04 AM
  4. Please solve my error occured java.lang.NullPointerException
    By Viruthagiri in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-24-2010, 12:08 PM
  5. Error occured during initialization of VM
    By red in forum New To Java
    Replies: 4
    Last Post: 10-02-2008, 07:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •