Results 1 to 10 of 10
  1. #1
    CCarr518 is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default "Could not find the main class"

    Hello,
    I am very new to java and recently attempted to run my first program. I created a simple program I found online in notepad

    Java Code:
     class HelloWorldApp {
        public static void main(String[] args) {
            System.out.println("Hello World!");
        }
    }
    I saved this as "HelloWorldApp.java" and then went to my command prompt, went to the correct location, complied it using "javac HelloWorldApp.java", made sure the "HelloWorldApp.class" file was present, and then attempted to run the program by typing "java HelloWorldApp". This gave me an error saying

    C:\Documents and Settings\Chris\My Documents\School>java HelloWorldApp
    Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp
    Caused by: java.lang.ClassNotFoundException: HelloWorldApp
    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. Program will exit.
    I don't know what to do to fix this issue. I have found similar issues being fixed by changing the Environmental Variables, but I already changed the PATH and CLASSPATH to "C:\Program Files\Java\jdk1.6.0_21\bin".

    Any help would be greatly appreciated!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default

    First let me congratulate you on an excellent posting of your question.

    If the HelloWorldApp.class file was in the School folder then the java command should find it.
    Your CLASSPATH environment variable shouldn't refer to the JDK's bin folder. Either delete that variable or add to it the current folder (do that with a .) For example:
    CLASSPATH=D:\JavaDevelopment;.

    To see your current settings, open a command prompt window and enter: Set
    If you want to copy the contents of that window:
    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.

  3. #3
    Dipke is offline Member
    Join Date
    Aug 2010
    Location
    Leuven, Belgium
    Posts
    86
    Rep Power
    0

    Default

    try
    Java Code:
    Java -cp . HelloWorldApp
    Add
    Java Code:
    .;
    to your CLASSPATH

    The point checks the current directory.

  4. #4
    CCarr518 is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default Current Settings

    Microsoft Windows XP [Version 5.1.2600]
    (C) Copyright 1985-2001 Microsoft Corp.

    C:\Documents and Settings\Chris>Set
    ALLUSERSPROFILE=C:\Documents and Settings\All Users
    APPDATA=C:\Documents and Settings\Chris\Application Data
    asl.log=Destination=file;OnFirstLog=command,enviro nment
    CLASSPATH=.
    CLIENTNAME=Console
    CommonProgramFiles=C:\Program Files\Common Files
    COMPUTERNAME=DELL-DESKTOP
    ComSpec=C:\WINDOWS\system32\cmd.exe
    FP_NO_HOST_CHECK=NO
    HOMEDRIVE=C:
    HOMEPATH=\Documents and Settings\Chris
    LOGONSERVER=\\DELL-DESKTOP
    NUMBER_OF_PROCESSORS=1
    OS=Windows_NT
    Path=C:\Program Files\Java\jdk1.6.0_21\bin;C:\Program Files\Java\jdk1.6.0_21\bin

    PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WS F;.WSH
    PROCESSOR_ARCHITECTURE=x86
    PROCESSOR_IDENTIFIER=x86 Family 15 Model 2 Stepping 9, GenuineIntel
    PROCESSOR_LEVEL=15
    PROCESSOR_REVISION=0209
    ProgramFiles=C:\Program Files
    PROMPT=$P$G
    QTJAVA=C:\Program Files\Java\jre6\lib\ext\QTJava.zip
    SESSIONNAME=Console
    SystemDrive=C:
    SystemRoot=C:\WINDOWS
    TEMP=C:\DOCUME~1\Chris\LOCALS~1\Temp
    TMP=C:\DOCUME~1\Chris\LOCALS~1\Temp
    USERDOMAIN=DELL-DESKTOP
    USERNAME=Chris
    USERPROFILE=C:\Documents and Settings\Chris
    windir=C:\WINDOWS

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default

    Are you able to execute the class now with the java command?

    I wonder why the path variable has the same path two times?

  6. #6
    CCarr518 is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default

    I am now able to run the program. Dipke's suggestion of using

    Java Code:
    Java -cp . HelloWorldApp
    instead of

    Java Code:
    java HelloWorldApp
    worked. I did not change anything in the environmental variables, my CLASSPATH is still "C:\Program Files\Java\jdk1.6.0_21\bin", as is PATH. I am very appreciative of all the help you have both given me but I am still slightly confused on what environmental variables actually are and why some people have suggested adding "." or ".;" in the CLASSPATH. Any explanation would be greatly appreciated

  7. #7
    CCarr518 is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default

    Also, what is the difference between adding "-cp ." and just the plain "java" command. I have never seen "java -cp ." in any other literature before

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default

    I think you need to use the -cp . option to refer to the current directory because your CLASSPATH environ variable: "C:\Program Files\Java\jdk1.6.0_21\bin" does NOT include it.
    I think the java command uses the CLASSPATH env var if given for the classpath. If it does not have the . for current directory, it does NOT look there.
    If you delete the CLASSPATH env var, I think java will default to the current directory.

  9. #9
    CCarr518 is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default

    Okay, thanks a lot!

  10. #10
    Dipke is offline Member
    Join Date
    Aug 2010
    Location
    Leuven, Belgium
    Posts
    86
    Rep Power
    0

    Default

    -cp is an abreviation for CLASSPATH.

    This option (-cp .) adds the given path (.) to the current active CLASSPATH.
    It is better to add the point to the CLASSPATH.
    So java will search each time automaticily the current directory.

    This issue is not written in every explanation of the CLASSPATH.

    Kind regards

Similar Threads

  1. Replies: 3
    Last Post: 08-25-2010, 11:21 AM
  2. Replies: 5
    Last Post: 06-22-2010, 04:51 PM
  3. Replies: 6
    Last Post: 05-18-2010, 06:11 PM
  4. Replies: 1
    Last Post: 11-20-2009, 08:58 PM
  5. Replies: 1
    Last Post: 06-30-2009, 03:32 PM

Tags for this Thread

Posting Permissions

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