I managed to figure out my hello world. And it worked! THen I decided to move my file directory for my java and class files so it would be easier to access long term. Now, I can't seem to get it to work again. I've altered the CLASSPATH for the new directory and have changed directory in the command prompt but no luck. I deleted the class file I'd created and recreated that--it worked--but when I run the class file I now have a nice long error.
My Entry: java MyFirstApp
Exception in thread "main" java.lang.NoClassDefFoundError: MyFirstApp Caused by: java.lang.ClassNotFoundException: MyFirstApp at java.net.URLClassLoader$1.run<URLClassLoader.java:200> at java.security.AccessController.doPrivileged<Native Method> at java.net.URLClassLoader.findClass<ClassLoader.java:307> at sun.misc.Launcher$AppClassLoader.loadClass<Launcher.java:301> at java.lang.ClassLoader.loadClass<ClassLoader.java:252> at java.lang.ClassLoader.loadClassInternal<ClassLoader.java:320> Could not find the main class MyFirstApp. Program will exit.
Maybe it's just too late in the day for me to be thinking. My guess is I made a mistake in moving the file somehow, but I've checked everything I can think of. Any thoughts based on this error?
After java.zip in your CLASSPATH, is that .; or ;.;?
Joined: Oct 13, 2005
There is no end of confusion about PATH and CLASSPATH, compounded by the fact that people remember older versions of Java where you did need to set a CLASSPATH.
PATH is where the operating system looks for the java program. If you are getting error messages from the java program your PATH must be correct. The two lines you quoted look like correct PATHs. The only problem you might have is that you will have to update them both whenever you upgrade to a newer version of Java.
CLASSPATH is where the java program looks for .class files. Since you have a . in your system CLASSPATH, it ought to work. Try writing PATH %PATH% CLASSPATH and %CLASSPATH% at the command line and see what happens. Those (or some of them!) ought to show you what the computer thinks the PATH and CLASSPATH are.
Most likely explanations for the error message you are getting:
You are not in the same folder as the .class file. Try the dir command to find the .class file.
Instruction misspelt; it is very fussy and case-sensitive, so the tiniest error will stop it working.
I presume you don't have a package statement in any of your files? It is different if you have a package statement. You run your file by
Navigating to the folder where your .java file resides
Entering javac MyFirstApp.java
Then java MyFirstApp
You can use the dir command at any point to find whether the files actually exist.
Joined: Nov 29, 2008
Thank you to everyone who's responded so far. I tried retyping, checking for typos, none I see. The javac command still seems to work, but not java. I typed PATH in the command prompt and got what seemed correct, however when I typed CLASSPATH I got the message:
'CLASSPATH' is not recognized as an internal or external command, operable program or batch file.
Just for the record, i tried it in all caps and all lower case.
Also, just to try, I renamed my folder "JavaFiles" from "Java Files" in case the space caused a problem (and updated the CLASSPATHs to reflect it.
I am running Vista, I don't think I mentioned that earlier - is there some known Vista problem?
Joined: Nov 29, 2008
Okay, upon further research I'm pretty sure it has something to do with CLASSPATH. I deleted the CLASSPATH reference entirely, and now, as long as I'm in the correct directory I'm fine. I'll stick with that for now, unless someone knows how my CLASSPATH could be fixed.
Originally posted by Michelle Noonan: ... I deleted the CLASSPATH reference entirely, and now, as long as I'm in the correct directory I'm fine...
That's exactly how it's supposed to work. If no CLASSPATH is specified, Java's default is to look in the current directory.
When you get to the point of needing classes in other directories, you will learn about using the classpath flag when compiling and running. At that point, the whole concept should start making more sense, and you will appreciate the drawbacks of having a system CLASSPATH set.
In the meantime, welcome to JavaRanch! [ November 30, 2008: Message edited by: marc weber ]