Results 1 to 7 of 7
Thread: Problem with main class
- 12-14-2010, 01:56 PM #1
Problem with main class
Ok, so I'm having a kind of a silly problem, which might have a very simple solution but apparently I don't see it.
My java compiler does not see my main class.
I make a folder, write a simple class:
class Test{
public static void main(String[] args){
System.out.println("Hello World");
}
}
in cmd I get to the folder, compile (C:\Test> javac Test.java), works without any problmes, and creates the Test.class. While in the folder i run "java Test" and get :
C:\test>java Test
Exception in thread "main" java.lang.NoClassDefFoundError: Test
Caused by: java.lang.ClassNotFoundException: Test
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 java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: Test. Program will exit.
I don't think its a path issue, since I'm IN the folder trying to run it...any ideas will be appriciated.
(btw, I have no problems running it through eclipse)
- 12-14-2010, 02:14 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
First, is that all your code?
There's no package declaration you're not showing us is there?
Also, is there a Test.class file in that folder after compiling?
- 12-14-2010, 02:16 PM #3
yep, thats all the code, no packages or anything else.
And yes, after the code compiles Test.class is created in the same folder.
- 12-14-2010, 03:27 PM #4
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
What does "echo $CLASSPATH" (or "echo %CLASSPATH%" on Windows) show? If there is no CLASSPATH then you should be able to execute a .class file in the current directory, but if you do have a CLASSPATH and . (the current directory) is not part of it, then you can't.
-Gary-
- 12-14-2010, 03:40 PM #5
Wow. That was a classic example of a bran-fart on my end! I actually spent hours trying to fix it (as originally the problem seemed a lot more complex before i narrowed it down to not being able to run a hello world class:D )
But yes, you are absolutely correct, my classpath was pointing to a jade.jar (which is a really cool Java Agent DEvelopment Framework, btw). Adding my test location to the classpath fixed the problem.
Thank you so much! I feel silly :)
- 12-14-2010, 03:43 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Why not just add '.'?
Because otherwise you'll encounter this again.
Also you really don't want to be adding your jar files and all that to the system CLASSPATH. That's what the -cp option is for with java and javac.
- 12-14-2010, 03:46 PM #7
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Similar Threads
-
Running main method class from another main class
By tlrocketman in forum New To JavaReplies: 3Last Post: 12-06-2010, 08:30 AM -
how do i call a class from main class
By revanger in forum New To JavaReplies: 7Last Post: 07-20-2010, 05:32 PM -
"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 -
different multiple public class and main class
By mr idiot in forum New To JavaReplies: 2Last Post: 01-01-2009, 12:10 PM -
How to create main class link to another two class?
By pearllymary78 in forum New To JavaReplies: 6Last Post: 07-16-2008, 11:02 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks