Results 1 to 18 of 18
  1. #1
    jbean079 is offline Member
    Join Date
    Apr 2012
    Location
    austin, tx
    Posts
    9
    Rep Power
    0

    Default Could not find or load main class (Windows 7 command line)

    Dear Forum members:

    I have trouble to run .java file under command prompt in Windows 7.
    JDK version: jdk1.7.0_02
    When I typed in java MyFirstClass, I received following error message
    C:\Users\admin121\workspace\helloworld\src\hellowo rld>java MyFirstClass
    Error: Could not find or load main class MyFirstClass
    I tried javac -cp option i received following error message
    C:\Users\admin121\workspace\helloworld\src\hellowo rld>javac -cp MyFirstClass.java
    javac: no source files
    Error: Could not find or load main class MyFirstClass.java
    Usage: javac <options> <source files>
    use -help for a list of possible options
    I use Eclipse run MyFirstClass.java it works fine.
    • the .classpath file is inside a parent folder 'helloworld'.
    • src folder inside parent 'helloworld' file contains another 'helloworld' folder.
    • the child 'helloworld' folder has 2 files: MyFirsclass.java and MyFirstClass.class

    when I run javac MyFirstclass.java in command prompt, the system generated MyFirstClass.class file

    Path and CLASSPATH environment variable configuration
    I edited the path environment variable by adding C:\Program Files\Java\JDK1.7.0_02\bin, then javac MyFirstClass.java worked.
    I created CLASSPATH variable with value %JAVA_HOME%\jre\lib (I removed JAVA_HOME variable)
    should I edit CLASSPATH variable by writing out full path name like C:\Program Files\Java\JDK1.7.0_02\jre\bin?

    base on Setting the class path this link. it suggest using java -cp option for each individual application, so it won't affect other applications.



    I would like to know which option is better: setting value for CLASSPATH variable or by using java cp <source file> option in the command prompt at run time?

    I search the internet for the similar issues, but I haven't find the solution to my problems. Please give me some advises on how to make it work properly.
    Last edited by jbean079; 04-23-2012 at 12:53 PM.

  2. #2
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: Could not find or load main class (Windows 7 command line)

    I'm still very new to Java, but from the tutorials I did it said not to put .java after the file name when you are running the class file, and that's what worked for me. So: C:\Users\admin121\workspace\helloworld\src\hellowo rld>java MyFirstClass

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Could not find or load main class (Windows 7 command line)

    Quote Originally Posted by jbean079 View Post
    I would like to know which option is better: setting value for CLASSPATH variable or by using java cp <source file> option in the command prompt at run time?
    The second. But it's not <source file>, it's the package root(s) and jars.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    jbean079 is offline Member
    Join Date
    Apr 2012
    Location
    austin, tx
    Posts
    9
    Rep Power
    0

    Default Re: Could not find or load main class (Windows 7 command line)

    Quote Originally Posted by Zigster View Post
    I'm still very new to Java, but from the tutorials I did it said not to put .java after the file name when you are running the class file, and that's what worked for me. So: C:\Users\admin121\workspace\helloworld\src\hellowo rld>java MyFirstClass
    Thanks for the reply Zigster:

    I had typo in the quoted text for error message. I didn't add .java when when using java MyFirstClass to run the program.
    In my helloworld folder: I have MyFirstClass.class and MyFirstClass.java file

  5. #5
    jbean079 is offline Member
    Join Date
    Apr 2012
    Location
    austin, tx
    Posts
    9
    Rep Power
    0

    Default Re: Could not find or load main class (Windows 7 command line)

    Quote Originally Posted by DarrylBurke View Post
    The second. But it's not <source file>, it's the package root(s) and jars.
    hello db:

    Thanks for the reply. In my case, what is the package roots and jars for the MyFirstClass.java file?
    Could you take a look at my original post and tell me what went wrong when I used javac -cp option?
    Thanks for your help.

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

    Default Re: Could not find or load main class (Windows 7 command line)

    When you entered the command: java MyFirstClass
    Where was the MyFirstClass.class file?
    Was it in the same directory as where you issued the java command? Use the dir command to display the directory's contents.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    jbean079 is offline Member
    Join Date
    Apr 2012
    Location
    austin, tx
    Posts
    9
    Rep Power
    0

    Default Re: Could not find or load main class (Windows 7 command line)

    Quote Originally Posted by Norm View Post
    When you entered the command: java MyFirstClass
    Where was the MyFirstClass.class file?
    Was it in the same directory as where you issued the java command? Use the dir command to display the directory's contents.
    Hello Norm:
    Thanks for the reply. I initially create the workspace and the project by using Elipse.
    Please help me to resolve the issue.

    I copied and pasted MyFirstclass.class and MyFirstClass.java file into parent folder (helloworld) or \src folder and run the same
    command I got same error message

    Parent directory (helloworld) content
    C:\Users\admin121\workspace\helloworld>dir
    Volume in drive C has no label.
    Volume Serial Number is 06A0-1962

    Directory of C:\Users\admin121\workspace\helloworld

    04/22/2012 08:42 AM <DIR> .
    04/22/2012 08:42 AM <DIR> ..
    02/07/2012 11:29 PM 301 .classpath
    02/07/2012 11:29 PM 386 .project
    02/07/2012 11:29 PM <DIR> .settings
    04/15/2012 10:01 AM <DIR> bin
    04/22/2012 08:42 AM 443 MyFirstClass.class
    02/08/2012 12:10 AM 225 MyFirstClass.java
    04/22/2012 08:30 AM <DIR> src
    4 File(s) 1,355 bytes
    5 Dir(s) 194,006,331,392 bytes free
    in the \src directory I have another directory called helloworld
    Directory of C:\Users\admin121\workspace\helloworld\src\hellowo rld

    04/21/2012 08:12 AM <DIR> .
    04/21/2012 08:12 AM <DIR> ..
    04/23/2012 05:41 AM 443 MyFirstClass.class
    02/08/2012 12:10 AM 225 MyFirstClass.java
    2 File(s) 668 bytes
    2 Dir(s) 194,006,310,912 bytes free
    Last edited by jbean079; 04-23-2012 at 02:01 PM.

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

    Default Re: Could not find or load main class (Windows 7 command line)

    What happens when you use the java command in the same folder that has the class file?
    Your last post does not show. Can you do a dir and then the java command in the same window?
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    jbean079 is offline Member
    Join Date
    Apr 2012
    Location
    austin, tx
    Posts
    9
    Rep Power
    0

    Default Re: Could not find or load main class (Windows 7 command line)

    Quote Originally Posted by Norm View Post
    What happens when you use the java command in the same folder that has the class file?
    Your last post does not show. Can you do a dir and then the java command in the same window?
    Hello Norm:

    Thanks for following up with me. Here is the copy for the command prompt. I hope this can help you identify the problem.
    C:\Users\admin121\workspace\helloworld\src\hellowo rld>dir
    Volume in drive C has no label.
    Volume Serial Number is 06A0-1962

    Directory of C:\Users\admin121\workspace\helloworld\src\hellowo rld

    04/21/2012 08:12 AM <DIR> .
    04/21/2012 08:12 AM <DIR> ..
    04/23/2012 05:41 AM 443 MyFirstClass.class
    02/08/2012 12:10 AM 225 MyFirstClass.java
    2 File(s) 668 bytes
    2 Dir(s) 194,522,427,392 bytes free

    C:\Users\admin121\workspace\helloworld\src\hellowo rld>java MyFirstClass
    Error: Could not find or load main class MyFirstClass

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

    Default Re: Could not find or load main class (Windows 7 command line)

    I am unable to create the condition that gives that error message.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Could not find or load main class (Windows 7 command line)

    1) Does MyFirstClass.java start with a package statement?

    2) What is the result of entering "CLASSPATH" on the command line?

    3) what happens if you give this command?
    Java Code:
    java -cp . MyFirstClass
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  12. #12
    jbean079 is offline Member
    Join Date
    Apr 2012
    Location
    austin, tx
    Posts
    9
    Rep Power
    0

    Default Re: Could not find or load main class (Windows 7 command line)

    Hello db:
    Thanks for the reply. I hope with forum member's help, i can solve this problem.

    Quote Originally Posted by DarrylBurke View Post
    1) Does MyFirstClass.java start with a package statement?
    I don't have a package statement within the MyFirstClass.java file

    2) What is the result of entering "CLASSPATH" on the command line?
    when I typed 'CLASSPATH' in command line I got following error message
    C:\Users\admin121>echo %CLASSPATH%
    C:\Program Files\Java\jdk1.7.0_02\jre\lib

    C:\Users\admin121>CLASSPATH
    'CLASSPATH' is not recognized as an internal or external command,
    operable program or batch file.

    3) what happens if you give this command?
    Java Code:
    java -cp . MyFirstClass
    the command prompt gave me the following error message
    C:\Users\admin121\workspace\helloworld\src\hellowo rld>java -cp . MyFirstClass
    Exception in thread "main" java.lang.NoClassDefFoundError: MyFirstClass (wrong name: helloworld/MyFirstClass)
    at java.lang.ClassLoader.defineClass1(Native Method)
    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$100(Unknown Source)
    at java.net.URLClassLoader$1.run(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)
    at sun.launcher.LauncherHelper.checkAndLoadMain(Unkno wn Source)

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Could not find or load main class (Windows 7 command line)

    Post the entire code of MyFirstClass.java. Don't leave out any lines.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default Re: Could not find or load main class (Windows 7 command line)

    Follow the advice given in the Oracle link and seconded by db: Remove the CLASSPATH environment variable. Use -cp instead.

    One effect of setting the CLASSPATH is that the java executable will attempt to use it and not the current directory when looking for the class you specify. It won't find it at the CLASSPATH location and that will lead to the "Could not find or load main class ..." which you mentioned in your first post.

    "C:\Program Files\Java\jdk1.7.0_02\jre\lib" is a strange value for the CLASSPATH variable in any case. And was *not* suggested in that page to which you linked.

    -----

    Exception in thread "main" java.lang.NoClassDefFoundError: MyFirstClass (wrong name: helloworld/MyFirstClass)
    This strongly suggests a package problem. Please post the contents of the MyFirstClass.java file.

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

    Default Re: Could not find or load main class (Windows 7 command line)

    java.lang.NoClassDefFoundError: MyFirstClass (wrong name: helloworld/MyFirstClass)
    The class is in the helloworld package which changes the requirements for contents of the commandline.
    The quick and easy fix is to remove the package helloworld; statement from the program and try again.
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    jbean079 is offline Member
    Join Date
    Apr 2012
    Location
    austin, tx
    Posts
    9
    Rep Power
    0

    Default Re: Could not find or load main class (Windows 7 command line)

    Quote Originally Posted by Norm View Post
    The class is in the helloworld package which changes the requirements for contents of the commandline.
    The quick and easy fix is to remove the package helloworld; statement from the program and try again.
    Hello Norm, pbrockway2 and db:

    I appreciate your guys' advises and step by step instruction to help me find the cause of the problem.
    I removed the package statement (package helloworld;) in my code, then compile and run MyFirstClass.java. It worked like a charm:)

    Here is my original code:

    Code:
    package helloworld; //trouble caused by the package statement
    
    public class MyFirstClass 
    {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) 
    	{
    		// TODO Auto-generated method stub
            System.out.println("Hello Eclipse!");
    	}
    
    }
    Last edited by jbean079; 04-25-2012 at 05:32 AM.

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

    Default Re: Could not find or load main class (Windows 7 command line)

    In post #12 you said: I don't have a package statement within the MyFirstClass.java file
    Strange that you were able to remove something that was not there and have the code work.
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    jbean079 is offline Member
    Join Date
    Apr 2012
    Location
    austin, tx
    Posts
    9
    Rep Power
    0

    Default Re: Could not find or load main class (Windows 7 command line)

    Quote Originally Posted by Norm View Post
    In post #12 you said: I don't have a package statement within the MyFirstClass.java file
    Strange that you were able to remove something that was not there and have the code work.
    Hello Norm:

    My apology to you and other moderators. I didn't carefully check my code at the time. I could resolved the issue earlier if I read my code thoroughly.

Similar Threads

  1. Replies: 3
    Last Post: 12-09-2011, 03:51 AM
  2. Error: Could not find or load main class
    By ITSlave in forum New To Java
    Replies: 4
    Last Post: 09-15-2011, 09:47 AM
  3. Cannot find main class Windows.
    By seegee in forum New To Java
    Replies: 24
    Last Post: 08-02-2011, 10:24 PM
  4. Replies: 3
    Last Post: 08-25-2010, 10:21 AM
  5. Replies: 1
    Last Post: 06-30-2009, 02:32 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
  •