Results 1 to 11 of 11
Thread: Problem with Java...
- 02-20-2010, 04:49 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 5
- Rep Power
- 0
Problem with Java...
Well my problem is as follows. Until today all my java files compiled and ran just fine, though all of a sudden today they compile and do not run instead giving me this error:
The code from said file is:Java Code:Exception in thread "main" java.lang.NoClassDefFoundError: hello Caused by: java.lang.ClassNotFoundException: hello at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:307) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:248) Could not find the main class: hello. Program will exit.
As you can see very very simple bit of code due to me being very new to this.Java Code:public class hello { public static void main (String args[]) { System.out.println("Hello"); } }
EDIT: After trying it in a command prompt and getting the error I decided to copy and paste the code directly into an IDE (JCreator) and it compiled and ran the code with no problems, I just can't seem to get it to work in the command prompt which is frustrating.Last edited by mc6415; 02-20-2010 at 05:15 PM.
- 02-20-2010, 08:44 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
The error means that the Java runtime cannot find the class hello. This is quite common - so don't panic!
What command are you using to launch the application?
Try something like:
Java Code:> dir > java -cp . main
Use these commands from the directory that contains the hello.class file. The first line - "dir" - is not necessary. It is just a sanity check which should verify that hello.class really does exist in the current directory. (Nonexistence or misspellings are a common source of NoClassDefFound)
The interesting bit is "dash-cp-space-dot-space". This tells the Java runtime where to look for classes. In this case it is saying "The classpath (the place to look for classes) is the current directory".
If this doesn't work post the commands and the exact and entire output they generate.
- 02-21-2010, 03:43 AM #3
Member
- Join Date
- Feb 2010
- Posts
- 5
- Rep Power
- 0
Hi there,
Thank you for replying, upon using your result I still got errors.
What I get is:
Thank you for the help :D.Java Code:Microsoft Windows [Version 6.1.7600] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\Coombes>cd \Users\Coombes\Documents\JavaStuff C:\Users\Coombes\Documents\JavaStuff>java -cp . attempt Exception in thread "main" java.lang.NoClassDefFoundError: attempt Caused by: java.lang.ClassNotFoundException: attempt at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:307) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:248) Could not find the main class: attempt. Program will exit. C:\Users\Coombes\Documents\JavaStuff>
- 02-21-2010, 05:10 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Do use the "dir" command as it helps to identify all sorts of simple errors like spelling mistakes.
In this case you would expect to see "attempt.class" listed among the files in your JavaStuff directory. If it not there then you have not compiled the program successfully.
- 02-21-2010, 02:45 PM #5
Member
- Join Date
- Feb 2010
- Posts
- 5
- Rep Power
- 0
Hi there,
I followed your instructions and my command prompt output was:
EDIT: Eureka, using java -cp . hello worked, I'm guessing that means I have a problem with my CLASSPATH setting.Java Code:Microsoft Windows [Version 6.1.7600] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\Coombes>cd \Users\Coombes\Documents\JavaStuff C:\Users\Coombes\Documents\JavaStuff>javac hello.java C:\Users\Coombes\Documents\JavaStuff>dir Volume in drive C has no label. Volume Serial Number is DC25-D911 Directory of C:\Users\Coombes\Documents\JavaStuff 21/02/2010 14:12 <DIR> . 21/02/2010 14:12 <DIR> .. 19/02/2010 15:36 <DIR> .metadata 19/02/2010 14:52 <DIR> book 20/02/2010 15:34 <DIR> DesktopApplication1 19/02/2010 15:40 <DIR> Hello World 19/02/2010 18:37 <DIR> Hello World2 21/02/2010 14:12 409 hello.class 20/02/2010 16:39 107 hello.java 19/02/2010 15:37 <DIR> HelloWorld3 19/02/2010 15:44 <DIR> Helloworldlul 20/02/2010 15:45 <DIR> JavaApplication3 2 File(s) 516 bytes 10 Dir(s) 23,533,543,424 bytes free C:\Users\Coombes\Documents\JavaStuff>java hello Exception in thread "main" java.lang.NoClassDefFoundError: hello Caused by: java.lang.ClassNotFoundException: hello at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:307) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:248) Could not find the main class: hello. Program will exit. C:\Users\Coombes\Documents\JavaStuff>
On another note what would I use to get keyboard input for a variable, that's not worded great but I basically want the equivalent of Python's variable=raw_input("Input variable: ") command in java.Last edited by mc6415; 02-21-2010 at 02:58 PM.
-
pbrockway's a genius, isn't he? But I wouldn't change your system's CLASSPATH data but rather the cp info when calling java as you're doing above.
Do you mean to dynamically set a variable's name during the running of a program? No, that's generally not done in Java. Instead you would use collections such as Lists and HashMaps to associated dynamically created objects.On another note what would I use to get keyboard input for a variable, that's not worded great but I basically want the equivalent of Python's variable=raw_input("Input variable: ") command in java.
- 02-21-2010, 03:25 PM #7
Member
- Join Date
- Feb 2010
- Posts
- 5
- Rep Power
- 0
Indeed I'm just gonna keep doing what's working and not play around with anything I don't need to until I'm comfortable with Java.
Ah I see, I'm still very new to this and it's hard to shake off assuming things that will work from my work with Python, I'll get used to it in the end.
-
- 02-21-2010, 03:46 PM #9
Member
- Join Date
- Feb 2010
- Posts
- 5
- Rep Power
- 0
- 02-22-2010, 12:19 AM #10
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Indeed I'm just gonna keep doing what's working and not play around with anything I don't need to until I'm comfortable with Java.
That sounds like a wise approach.
Act as if the CLASSPATH environment variable doesn't exist. I'm no expert - but that just allows me to to state more vehemently - it should have been deprecated years ago. And then removed from java and javac so that people who relied on it (Apple etc) would be forced to fix their broken software.
Hopefully you understand the explication I gave of the -cp argument:
"-cp . " == "look for classes in the current directory"
That provides the basis for more elaborate command line arguments as and when you need them.
-----
re Python's raw_input(). That's a way of obtaining a string from standard input. Java offers various ways of doing this, including one that's pretty much equivalent: the Console method readLine(). Details here.
The reference is to Oracle's Tutorial which is a must read. (Well as much of it as you need: it's quite comprehensive). Nearby sections on scanning and character streams are also useful, but are presented in terms of files because, unlike raw_input(), they are not limited to standard input.
-
Similar Threads
-
Java problem help!
By wexgal in forum New To JavaReplies: 3Last Post: 10-26-2009, 12:52 PM -
Java Problem. Need Help!
By bob101 in forum New To JavaReplies: 6Last Post: 03-19-2009, 04:34 AM -
JAVA and XML Problem
By jackchang in forum XMLReplies: 4Last Post: 02-22-2009, 08:28 PM -
problem with java nio
By andrei stoiculescu in forum NetworkingReplies: 3Last Post: 02-02-2009, 03:35 PM -
Java problem
By grend in forum New To JavaReplies: 5Last Post: 08-18-2008, 11:44 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks