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

    Default 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>

    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);};
    			
    		}
    
    		
    		
    	}
    In the other post we determined that Eclipse was not going to MainClass - can someone help me on this?

    Thanks,

    EdenC

    [Original Post Link]

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

    Default

    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.

  3. #3
    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, finish and call your jar. I could run your code successfully from a jar file.

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

    Default

    Thanks for all help so far!
    ----------------------------------------------------------------------------------------------------------------
    Quote Originally Posted by Norm View Post
    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.
    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++).
    ----------------------------------------------------------------------------------------------------------------
    Quote Originally Posted by j2me64 View Post
    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.
    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

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

    Default

    The only contents of Mainfest.mf is "Maifest-Version: 1.0"
    That is not enough. You need the Main-Class: entry.

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

    Default

    Quote Originally Posted by Norm View Post
    That is not enough. You need the Main-Class: entry.
    I know - is it OK to add this myself?

    Thanks,

    EdenC

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

    Default

    Quote Originally Posted by EdenC View Post
    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?
    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.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    Quote Originally Posted by j2me64 View Post
    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.
    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.

    Quote Originally Posted by j2me64 View Post
    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.
    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).

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

    Default

    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

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

    Default

    Quote Originally Posted by Tolls View Post
    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).
    If you want to use your own manifest file make sure there is a blank line as last line in your manifest file!

Similar Threads

  1. Java Exception Has Occured
    By EdenC in forum New To Java
    Replies: 12
    Last Post: 08-29-2011, 04:20 PM
  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
  •