Results 1 to 10 of 10
Thread: Class Not Found command promt
- 08-17-2009, 04:52 PM #1
Member
- Join Date
- Aug 2009
- Posts
- 8
- Rep Power
- 0
Class Not Found command promt
Hi,
I'm not really a beginner in Java, but since I've never worked from the command prompt before, I guess I am a beginner at that ;)
I've been trying to start a Java application from the command prompt, but I keep getting the error message that dhe class could not be found.
I've searched the internet a lot and tested things, but I can't figure out what the problem is. To make things simple I made a Hello World application which gives the same error.
I made the following program, naming it HelloWorld.java and saving it in the directory C:\mywork
I then started the command prompt and compiled the program usingJava Code:public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } }
javac HelloWorld.java
When I tried to run it using
java HelloWorld
I got the following error message:
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld
Caused by: <list of things>
Could not find the main class: HelloWorld. Program will exit.
What am I missing? Please help. I need to test my application, it's an assignment for school and I have to hand it in in a week...
- 08-17-2009, 08:34 PM #2
Member
- Join Date
- Aug 2009
- Posts
- 8
- Rep Power
- 0
Ok, I got some help elsewhere, but I don't understand what was wrong. So if someone could explain to me what the problem was, I would be grateful.
The solution was to start the application using the following
java -classpath . HelloWorld
I was told I had to change the CLASSPATH variable to solve the problem permanently, by putting a dot in front of the current CLASSPATH. But the dot was already in place.
Anyone have a clue as to why things are acting this way?
Edit: Oh, and I'm sorry for the double posting, but it is kind of a reply to my earlier message, not an addition or something ;)
- 08-17-2009, 09:00 PM #3
Member
- Join Date
- Aug 2009
- Posts
- 5
- Rep Power
- 0
depends on the operating system you have but I believe you just run your jar
java -jar myjar.jar
quick search on this should help a lot.
- 08-17-2009, 09:05 PM #4
Member
- Join Date
- Aug 2009
- Posts
- 8
- Rep Power
- 0
Thank you for your reply.
However, I don't have any jar-file. I was just working with .java and .class files.
And I did more than a quick search, I did a thorough search.
But as I said, the solution was something that shouldn't have worked. I had checked my classpath, but it seemed fine. So adding "-classpath . " shouldn't have changed anything.
- 08-17-2009, 09:32 PM #5
Print out the entire command line session of you trying to compile/run it so we can see everything.
Here is mine
Mr. BeansJava Code:joe@joe:~/Projects$ ls cdk Main.java joe@joe:~/Projects$ javac Main.java joe@joe:~/Projects$ ls cdk Main.class Main.java joe@joe:~/Projects$ java Main Hello World
- 08-17-2009, 09:40 PM #6
Member
- Join Date
- Aug 2009
- Posts
- 8
- Rep Power
- 0
Java Code:C:\mywork>javac HelloWorld.java C:\mywork>java HelloWorld Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld Caused by: java.lang.ClassNotFoundException: HelloWorld 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: HelloWorld. Program will exit. C:\mywork>java -classpath . HelloWorld Hello World! C:\mywork>I noticed that the path doesn't have a dot in front, but if I go to environment variables (properties of My Computer, Advanced tab) the classpath variable does have a dot...Java Code:C:\mywork>path PATH=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32\WBEM;C:\Program Files\Au todesk\Maya 8.5 Personal Learning Edition\bin;C:\Program Files\MiKTeX 2.7\miktex \bin;C:\Program Files\Common Files\Teleca Shared;E:\Programma's\TortoiseSVN\bin; C:\Program Files\QuickTime\QTSystem\;C:\Program Files\Java\jdk1.5.0_04\bin;E:\Pr ogramma's\OpenVPN\bin
- 08-17-2009, 09:46 PM #7
Member
- Join Date
- Aug 2009
- Posts
- 4
- Rep Power
- 0
Tip: Use the command set to see/edit current environment variables from the command line.
Java won't look for classes in the other, ordinary path, though the dot directory is implicit in that, by the way.
- 08-17-2009, 10:28 PM #8
Member
- Join Date
- Aug 2009
- Posts
- 8
- Rep Power
- 0
So I typed 'set' followed by Enter.
The path is as posted before. The classpath is '. (period)'
no idea what that means.
In what way should I edit which variable?
-
have you tried compiling like so:
(don't forget the period in there)Java Code:javac -cp . HelloWorld.java
Edit: also, are you placing your java file in a package and giving it a package name (my guess is not)?Last edited by Fubarable; 08-17-2009 at 10:59 PM.
- 08-18-2009, 12:36 AM #10
Member
- Join Date
- Aug 2009
- Posts
- 8
- Rep Power
- 0
Ok, I tried compiling with the -cp . added
When trying to run the application, I got the same errors, so I still had to add -cp . when running it.
And no, no package.
As I said, I am currently testing with simply a HelloWorld.java, which is really nothing more than a good old fashioned Hello World program. The code of this "program" is in my first post.
And thanks for the replies, I really appreciate you all trying to help :)
Similar Threads
-
Error: no class definition found
By toby in forum New To JavaReplies: 6Last Post: 08-28-2011, 10:32 PM -
Class not found Exception
By surendra in forum Java ServletReplies: 8Last Post: 06-09-2011, 12:52 PM -
No Class Def Found Error:
By fithous in forum Java AppletsReplies: 0Last Post: 04-02-2009, 05:30 PM -
JRE rtapplet class not found error
By avinash.natekar in forum Java AppletsReplies: 11Last Post: 04-02-2009, 08:02 AM -
No Class Def Found Error:
By wrap23 in forum New To JavaReplies: 9Last Post: 10-02-2008, 04:07 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks