Results 1 to 3 of 3
- 02-02-2011, 06:08 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 2
- Rep Power
- 0
Receiving error "Could not find the main class"
I have recently started learning Java...and I wrote first code of mine as below:
[Code]
public class Kalpesh
{
public static void main (String args[])
{
System.out.println("Kalpesh");
}
}
I also have set classpath and path variables correctly. The program compiles fine using command line:
javac C:\Kalpesh.java
But on executing, I receive below error:
java C:\Kalpesh
Error is displayed as:
C:\Program Files\Java\jdk1.6.0_23\bin>java C:\Kalpesh
Exception in thread "main" java.lang.NoClassDefFoundError: C:\Kalpesh
Caused by: java.lang.ClassNotFoundException: C:\Kalpesh
at java.net.URLClassLoader$1.run(URLClassLoader.java: 202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.j ava:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:3 07)
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:2 48)
Could not find the main class: C:\Kalpesh. Program will exit.
I tried using java -cp . Kalpesh command as well but still receiving same error. Can someone help me out?
- 02-02-2011, 06:17 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,717
- Rep Power
- 16
The java executable takes a class name as an argument - not a filename like the javac executable. So it doesn't really make sense to say
Java Code:java C:\Kalpesh
The usual way of running the program is
Java Code:java -cp . Kalpesh
You have tried this, but there are a few things you can check.
(1) The command must be issued from the directory containing Kalpesh.class
(2) The file Kalpesh.class must really be present in that directory. Ie the compilation must have been sucessful. Check this using the dir command
(3) The name of the class must really be Kalpesh. If the class belongs to a package then both the name of the class and the location where the command should be run will be different.
(4) The Kalpesh class must have a well defined "entry point". If you make a mistake in how you declare the main() method there will be a problem. If you are unsure about this, post the Kalpesh.java code
- 02-02-2011, 06:31 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 2
- Rep Power
- 0
Hey....
The issue is resolved...
You were correct that I needed to issue 'Java' command from the directory where my .class file resides. I was receiving error saying 'java is not recognized command....
I then set 'Path' variable to the directory containing .class file and the issue got resolved..
Thanks a lot for help..!!!
Similar Threads
-
Error message "could not find the main class"
By srwpchelp in forum New To JavaReplies: 8Last Post: 12-23-2010, 09:15 PM -
"Could not find the main class"
By CCarr518 in forum New To JavaReplies: 9Last Post: 09-25-2010, 06:11 PM -
"Could not find the main class" for Linux Command Line Compile
By tetelee in forum New To JavaReplies: 3Last Post: 08-25-2010, 10:21 AM -
"Could not find the main class: comparisonDemo.class. Program will exit."
By ziisrick in forum New To JavaReplies: 6Last Post: 05-18-2010, 05:11 PM -
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, 02:32 PM
Bookmarks