Results 1 to 12 of 12
  1. #1
    RepusJonathan is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Default Hello World: Could not find the main class

    Hello Everybody!

    I'm new and excited to be on this forum. I am a computer science major who learned Python as my first language and am now trying to pick up Java.
    However, I have become extremely frustrated because I cannot run a simple HelloWorld program even after hours trying to figure it out. Before, I could just launch any python program after feeding into the interpreter but for my HelloWorld program I get the following error:



    My code for the HelloWorld.java file is this
    Java Code:
    public class HelloWorld {
    	
    	public static void main (String[] args) {
    		System.out.println("Hello world!");
    		
    	}
    }
    1) When I had BOTH HelloWorld.java and HelloWorld.class (the compiled form of HelloWorld.java) in the SAME directory and tried to execute it, the following happened:
    Java Code:
    Jonathan@Envy /cygdrive/c/Users/Jonathan/Documents/Dropbox/Java/Helloworld
    $ java HelloWorldApp.class
    Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp/class
    Caused by: java.lang.ClassNotFoundException: HelloWorldApp.class
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
    Could not find the main class: HelloWorldApp.class.  Program will exit.
    2) I started a NEW HelloWorld project in Eclipse and when I ran it WITHIN Eclipse, it ran fine. HOWEVER, trying to run it via command prompt gave me the following error:
    Java Code:
    Jonathan@Envy /cygdrive/c/Users/Jonathan/workspace/HelloWorld/bin
    $ java HelloWorld.class
    Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld/class
    Caused by: java.lang.ClassNotFoundException: HelloWorld.class
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
    Could not find the main class: HelloWorld.class.  Program will exit.
    Why is it that I cannot run .class files myself but they only run when I do the whole project through Eclipse? It would be very cumbersome to have to do everything in Eclipse and not be able to edit code via my lovely vim.

    Also, could someone EXPLAIN what is going on a bit? This seems very odd for a python programmer who could used to just type "python <filenamehere.py>" and it would work. THANK so much! I've spent hours on this already and just want to move on learning more Java!

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default Re: Hello World: Could not find the main class

    Leave out the '.class' suffix when you try to run your class, i.e. 'java HelloWorld' should do it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    RepusJonathan is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Default Re: Hello World: Could not find the main class

    Thanks for your reply.

    However, it still doesn't work:


    Jonathan@Envy /cygdrive/c/Users/Jonathan/workspace/HelloWorld/bin
    $ java HelloWorld
    Exception in thread "main" java.lang.UnsupportedClassVersionError: HelloWorld : Unsupported major.minor version 51.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(Unknown Source)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknow n Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$000(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    Could not find the main class: HelloWorld. Program will exit.

    This is running the HelloWorld.class file that Ecplise just successfully ran within itself.

  4. #4
    RepusJonathan is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Default Re: Hello World: Could not find the main class

    Quote Originally Posted by JosAH View Post
    Leave out the '.class' suffix when you try to run your class, i.e. 'java HelloWorld' should do it.

    kind regards,

    Jos
    However, it still has the same error. and I am running the HelloWorld.class that was run successfully WITHIN Eclipse (compiled, written and ran within Eclipse, same code as all of my HelloWorld variants).

    Java Code:
    $ java HelloWorld
    Exception in thread "main" java.lang.UnsupportedClassVersionError: HelloWorld : Unsupported major.minor version 51.0
            at java.lang.ClassLoader.defineClass1(Native Method)
            at java.lang.ClassLoader.defineClassCond(Unknown Source)
            at java.lang.ClassLoader.defineClass(Unknown Source)
            at java.security.SecureClassLoader.defineClass(Unknown Source)
            at java.net.URLClassLoader.defineClass(Unknown Source)
            at java.net.URLClassLoader.access$000(Unknown Source)
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
    Could not find the main class: HelloWorld.  Program will exit.
    
    Jonathan@Envy /cygdrive/c/Users/Jonathan/workspace/HelloWorld/bin

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Hello World: Could not find the main class

    It looks like you are trying to run the program with an older version of Java than that for which the program was compiled.

    You can check the version of the java runtime with

    Java Code:
    java -version
    and set the version in Eclipse where it is called "Generated .class files compatability" in the project properties. However...

    It would probably be a good idea to step away from the compiler and compile and run at the command line for such a simple program. (That's the best way to learn the syntax of the commands.) If you have multiple Java runtimes, make sure the correct one is invoked (ie the most recent one should be the one on your path).

  6. #6
    RepusJonathan is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Default Re: Hello World: Could not find the main class

    Quote Originally Posted by pbrockway2 View Post
    It looks like you are trying to run the program with an older version of Java than that for which the program was compiled.

    You can check the version of the java runtime with

    Java Code:
    java -version
    and set the version in Eclipse where it is called "Generated .class files compatability" in the project properties. However...

    It would probably be a good idea to step away from the compiler and compile and run at the command line for such a simple program. (That's the best way to learn the syntax of the commands.) If you have multiple Java runtimes, make sure the correct one is invoked (ie the most recent one should be the one on your path).

    So in the COMMAND PROMPT when I type 'java -version' I get:
    Java Code:
    Jonathan@Envy /cygdrive/c/Users/Jonathan/workspace/HelloWorld/bin
    $ java -version
    java version "1.6.0_26"
    Java(TM) SE Runtime Environment (build 1.6.0_26-b03)
    Java HotSpot(TM) Client VM (build 20.1-b02, mixed mode, sharing)

    However my PATH environment variable only includes 1 java reference ( I deleted the other one) [ C:\Program Files\Java\jdk1.7.0_02\bin] HOWEVER, that does NOT match up with what I got when I typed in 'java -version'.

    But in Eclipse I am correctly using the latest version of Java to compile it (the JavaSE 1.7 version, unlike the 1.6 I get when I type in 'java -version').

    I am very confused on how come I can't get this simple program to compile in command prompt so I can continue learning more java.

  7. #7
    sandy73 is offline Member
    Join Date
    Dec 2011
    Posts
    1
    Rep Power
    0

    Default Re: Hello World: Could not find the main class

    I think your CLASSPATH is mistake. JVM is unable to find your .class file.
    Check your classpath once.
    If this doesn't work, see that your java PATH is placed before ORACLE PATH.

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default Re: Hello World: Could not find the main class

    Quote Originally Posted by sandy73 View Post
    I think your CLASSPATH is mistake. JVM is unable to find your .class file.
    Check your classpath once.
    If this doesn't work, see that your java PATH is placed before ORACLE PATH.
    Nope, it's not a classpath issue, it's a version issue, i.e. the JVM has an older version than the compiler and/or the classes the program was compiled against.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default Re: Hello World: Could not find the main class

    You seem to be running cygwin (on a MS Windows box). What do you see when you search for 'java.exe' in your 'Start' menu? I bet there's more than one java virtual machine installed without you realizing it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    RepusJonathan is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Default Re: Hello World: Could not find the main class

    Hey guys,

    So i've confirmed that the VERSIONS ARE THE SAME NOW after fixing thing up for BOTH Java and javac. There is ONLY ONE version now on my computer as I have deleted all other older versions of java (SDK and JRE).

    Java Code:
    Jonathan@Envy ~
    $ java -version
    java version "1.7.0_02"
    Java(TM) SE Runtime Environment (build 1.7.0_02-b13)
    Java HotSpot(TM) 64-Bit Server VM (build 22.0-b10, mixed mode)
    
    Jonathan@Envy ~
    $ javac -version
    javac 1.7.0_02


    However, I still get the same issue....
    Java Code:
    Jonathan@Envy /cygdrive/c/Users/Jonathan/Documents/Dropbox/Java/Helloworld
    $ ls
    HelloWorldApp.class  HelloWorldApp.java  HelloWorldApp.java~
    
    Jonathan@Envy /cygdrive/c/Users/Jonathan/Documents/Dropbox/Java/Helloworld
    $ javac HelloWorldApp.java
    
    Jonathan@Envy /cygdrive/c/Users/Jonathan/Documents/Dropbox/Java/Helloworld
    $ java HelloWorldApp
    Error: Could not find or load main class HelloWorldApp

  11. #11
    kennydupp is offline Member
    Join Date
    Jan 2012
    Posts
    1
    Rep Power
    0

    Default Re: Hello World: Could not find the main class

    I had the same problem. The issue was the version i downloaded was wrong.( I downloaded the 86x ), I was suppose to download the 64x version instead. After I downloaded the 64x version, I went back to fix my path "file location" to the 64x version and it all went well after that. Hope this helps.

  12. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Hello World: Could not find the main class

    It seemed the OP had fixed the original problem (version incompatibility).

    The last post involved new code (that was never posted) for HelloWorldApp. It is quite consistent with everything in this thread that the new (/second/different) problem was that of a faulty classpath.

    Java Code:
    Jonathan@Envy /cygdrive/c/Users/Jonathan/Documents/Dropbox/Java/Helloworld
    $ ls
    HelloWorldApp.class  HelloWorldApp.java  HelloWorldApp.java~
     
    Jonathan@Envy /cygdrive/c/Users/Jonathan/Documents/Dropbox/Java/Helloworld
    $ javac HelloWorldApp.java
     
    Jonathan@Envy /cygdrive/c/Users/Jonathan/Documents/Dropbox/Java/Helloworld
    $ java HelloWorldApp
    Error: Could not find or load main class HelloWorldApp
    
    Jonathan@Envy /cygdrive/c/Users/Jonathan/Documents/Dropbox/Java/Helloworld
    $ java -cp . HelloWorldApp
    The last command there is a somewhat safer way of executing HelloWorldApp.

    An alternative is that HelloWorldApp.java does not contain a main() method with the right signature - we just don't know based on what's been posted.

Similar Threads

  1. Replies: 1
    Last Post: 11-06-2011, 12:26 AM
  2. Could not find or load main class BubbleSort.class
    By blaqkout in forum New To Java
    Replies: 5
    Last Post: 09-12-2011, 07:54 PM
  3. Could not find main class
    By Karenthian in forum New To Java
    Replies: 7
    Last Post: 03-30-2011, 03:02 PM
  4. Could Not Find Main Class (again)
    By illjavathat in forum New To Java
    Replies: 25
    Last Post: 11-23-2010, 09:49 AM
  5. could not find main class?
    By deathnote202 in forum New To Java
    Replies: 6
    Last Post: 05-10-2010, 08:57 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
  •