Results 1 to 10 of 10
  1. #1
    vietnamusa is offline Member
    Join Date
    Dec 2010
    Posts
    8
    Rep Power
    0

    Default Could not find the main class: HelloWorld. Program will exit.

    Hello everyone,

    I just wrote a very simple Java program by using Notepad in Windows Vista Home Premium 32-bit Operating System:

    public class HelloWorld
    {
    public static void main(String [] args)
    {
    System.out.println("Hello World");
    }
    }

    I saved it as HelloWorld.java in C:\Temp\ folder.
    Then from cmd window, I went to C:\Temp\ folder.
    I compiled this .java file and there was no errors. But I got errors when I ran this program.

    C:\Temp>javac HelloWorld.java

    C:\Temp>java HelloWorld
    Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld
    Caused by: java.lang.ClassNotFoundException: HelloWorld
    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.

    C:\Temp>java -version
    java version "1.6.0_22"
    Java(TM) SE Runtime Environment (build 1.6.0_22-b04)
    Java HotSpot(TM) Client VM (build 17.1-b03, mixed mode, sharing)

    C:\Temp>

    Please tell me what I did wrong and how to fix it. It is just a very simple Java program that prints "Hello World"

    Thank you

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    it might be that the 'classpath' needs to be pointing to the c:\temp folder, because even though your current working directory is this folder, the Java vm by default does not know to look in this folder for loading classes.

    try:

    Java Code:
    java -classpath . HelloWorld
    from inside the C:\Temp folder where the HelloWorld.class file is.

  3. #3
    baloda is offline Member
    Join Date
    Nov 2010
    Location
    New Delhi
    Posts
    50
    Rep Power
    0

    Default

    another simple trick is

    put ur HelloWorld.java file to C:\Program Files\Java\jdk1.6.0_22\bin folder (can change according ur java version and local settings )

    and then compile it again.

  4. #4
    vietnamusa is offline Member
    Join Date
    Dec 2010
    Posts
    8
    Rep Power
    0

    Default

    I tried travishein's suggestion and baloda's suggestion and both work.
    Thank you very much for your help :-)

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

    Default

    Quote Originally Posted by baloda View Post
    another simple trick is

    put ur HelloWorld.java file to C:\Program Files\Java\jdk1.6.0_22\bin folder (can change according ur java version and local settings )
    Better don't do that.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Join Date
    Dec 2010
    Posts
    1
    Rep Power
    0

    Default

    Hi all,

    My first post here : And since i am an absolute beginner, i would not want to clutter the forum with a naive thread. 'vietnamusa' - I apologize for hijacking your thread, but since our questions kinda sounds similar, i am just making use of this thread.Thanks.

    So, i downloaded the JDK ( "1.6.0_23"). This is my installation path : C:\Program Files\Java\jdk1.6.0_23\bin. I created the now mandatory 'helloworld' program using notepad and named it helloworld.java. This program is under C:\Program Files\Java\myprograms. I have two problems now :

    a) Although i did set the path permanently, when i try to compile the program through cmd prompt, i still get a 'javac not a valid command' message.
    b) If i place the helloworld.java right under the \bin (where my javac is located) and do a :
    C:\Program Files\Java\jdk1.6.0_23\bin>javac helloworld.java
    This is what i get :

    javac: file not found: helloworld.java
    Usage: javac <options> <source files>
    use -help for a list of possible options

    Where am i going wrong? Thanks all.

  7. #7
    baloda is offline Member
    Join Date
    Nov 2010
    Location
    New Delhi
    Posts
    50
    Rep Power
    0

    Default

    @manfrmliverpool

    thing that might be in ur case is:

    Save ur file using "HelloWorld.java" in notepad.

    coz sometimes u save ur file as .java it becomes HelloWorld.java.txt

    so ur compiler can't find the proper .java file with HelloWorld.java name.
    Last edited by baloda; 12-22-2010 at 12:57 PM.

  8. #8
    baloda is offline Member
    Join Date
    Nov 2010
    Location
    New Delhi
    Posts
    50
    Rep Power
    0

    Default

    Dear Sir JosAH ,

    can u please explain why one should not do that???

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

    Default

    Quote Originally Posted by baloda View Post
    Dear Sir JosAH ,

    can u please explain why one should not do that???
    It makes that directory messy (executable files should not be stored together with user Java source files), the directory might not even be writable and changing versions of Java causes a large overhaul of your source files to the new Java installation directory. There are obviously more reasons not to do it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    if you are developing with the same java sources folder for all of your learning and hello world kinds of things, in the control panel, environment variables, you can set an an environment variable for your user for CLASSPATH to be this folder. (along with adding the program_files/java/bin folder to the path.

    another idea, is to create a set environment variables .bat file
    so that every time you open a cmd, you run this .bat file in your sources folder.

    Java Code:
    set JAVA_HOME=C:\Program Files\Java\jdk1.6.0_23
    set PATH=%PATH%;%JAVA_HOME%\bin
    set CLASSPATH=C:\Program Files\Java\myprograms
    Where the values for C:\... will likely be slightly different depending on where your JDK is installed and its version, and where you keep 'your programs'.

    But now, the javac command should just be found and running java MyClass should also just work in this cmd shell.

    When I was taking courses, i had a setenv.bat file in each assignment folder, i just copied it forward and changed the CLASSPATH=C:\data\mycourse\assignment4\ and so on. That way, each assignment, or wave of hello world types of things had their own individual environment, which was handy because sometimes i woud have the same class at different points of time as I learned more things, and it was handy to go back to the time before when the class was simpler.

Similar Threads

  1. Replies: 2
    Last Post: 09-22-2011, 10:48 PM
  2. Could not find the main class, program will exit
    By grottesco in forum New To Java
    Replies: 7
    Last Post: 11-03-2010, 07:51 AM
  3. Could not find the main class, program will exit.
    By aryubi in forum New To Java
    Replies: 39
    Last Post: 02-19-2010, 11:02 AM
  4. Could not find the main class. Program will exit.
    By XSROSSITER in forum New To Java
    Replies: 0
    Last Post: 01-30-2010, 05:17 AM
  5. Could not find the main class. Program will exit.
    By Carrier in forum New To Java
    Replies: 1
    Last Post: 04-22-2009, 03:27 AM

Posting Permissions

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