Results 1 to 18 of 18
- 04-23-2012, 09:19 AM #1
Member
- Join Date
- Apr 2012
- Location
- austin, tx
- Posts
- 9
- Rep Power
- 0
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
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
- 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 01:53 PM.
- 04-23-2012, 09:53 AM #2
Member
- Join Date
- Apr 2012
- Posts
- 74
- Rep Power
- 0
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
- 04-23-2012, 10:06 AM #3
- 04-23-2012, 02:07 PM #4
Member
- Join Date
- Apr 2012
- Location
- austin, tx
- Posts
- 9
- Rep Power
- 0
Re: Could not find or load main class (Windows 7 command line)
- 04-23-2012, 02:11 PM #5
Member
- Join Date
- Apr 2012
- Location
- austin, tx
- Posts
- 9
- Rep Power
- 0
Re: Could not find or load main class (Windows 7 command line)
- 04-23-2012, 02:31 PM #6
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.
- 04-23-2012, 02:59 PM #7
Member
- Join Date
- Apr 2012
- Location
- austin, tx
- Posts
- 9
- Rep Power
- 0
Re: Could not find or load main class (Windows 7 command line)
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
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 freeLast edited by jbean079; 04-23-2012 at 03:01 PM.
- 04-23-2012, 03:05 PM #8
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.
- 04-23-2012, 04:13 PM #9
Member
- Join Date
- Apr 2012
- Location
- austin, tx
- Posts
- 9
- Rep Power
- 0
Re: Could not find or load main class (Windows 7 command line)
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
- 04-23-2012, 05:19 PM #10
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.
- 04-23-2012, 05:28 PM #11
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
If you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 04-24-2012, 06:12 AM #12
Member
- Join Date
- Apr 2012
- Location
- austin, tx
- Posts
- 9
- Rep Power
- 0
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.
I don't have a package statement within the MyFirstClass.java file
2) What is the result of entering "CLASSPATH" on the command line?
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
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)
- 04-24-2012, 06:33 AM #13
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.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 04-24-2012, 06:46 AM #14
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
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)
- 04-24-2012, 01:59 PM #15
Re: Could not find or load main class (Windows 7 command line)
java.lang.NoClassDefFoundError: MyFirstClass (wrong name: helloworld/MyFirstClass)
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.
- 04-25-2012, 06:27 AM #16
Member
- Join Date
- Apr 2012
- Location
- austin, tx
- Posts
- 9
- Rep Power
- 0
Re: Could not find or load main class (Windows 7 command line)
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 06:32 AM.
- 04-25-2012, 01:52 PM #17
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.
- 04-25-2012, 04:42 PM #18
Member
- Join Date
- Apr 2012
- Location
- austin, tx
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
Please help! Cannot find or load main class error
By BenH in forum New To JavaReplies: 3Last Post: 12-09-2011, 04:51 AM -
Error: Could not find or load main class
By ITSlave in forum New To JavaReplies: 4Last Post: 09-15-2011, 10:47 AM -
Cannot find main class Windows.
By seegee in forum New To JavaReplies: 24Last Post: 08-02-2011, 11:24 PM -
"Could not find the main class" for Linux Command Line Compile
By tetelee in forum New To JavaReplies: 3Last Post: 08-25-2010, 11:21 AM -
Compiling/Running Project in Command Line: "Could not find main class" Error
By Yasemin Gokce in forum New To JavaReplies: 1Last Post: 06-30-2009, 03:32 PM
Bookmarks