wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Trouble with my Hello World Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trouble with my Hello World" Watch "Trouble with my Hello World" New topic
Author

Trouble with my Hello World

Michelle Noonan
Greenhorn

Joined: Nov 29, 2008
Posts: 5
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

Error:

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?

Thanks.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

try this... from the directory where you have your class file, run:

java -cp . MyFirstApp

(I think that's the correct syntax)

This tells the jvm to search the current directory for the class file. if this works, then there is a problem with your CLASSPATH.

Let us know what happens when you do this, and we'll go from there.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Michelle Noonan
Greenhorn

Joined: Nov 29, 2008
Posts: 5
Okay, I tried that and got this:

Usage: java [-options] class [args...]
<to execute a class>
or java [-options] -jar jarfile [args...]
<to execute a jar file>

where options include:
long list I'm not going to retype....


not sure if that meant it worked or not.
Michelle Noonan
Greenhorn

Joined: Nov 29, 2008
Posts: 5
Just to maybe move things along in case this is the problem, here are my exactly User variables:

CLASSPATH
.C:\Users\Michelle\Java Files

PATH
C:\Program Files\Java\jdk1.6.0_10\bin

and System variables:

CLASSPATH
.;C:\Program Files\Java\jre1.6.0_01\lib\ext\QTJava.zip .;C:\Users\Michelle\Java Files

PATH
C:\Program Files\Java\jdk1.6.0_10\bin
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
After java.zip in your CLASSPATH, is that .; or ;.;?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
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.
    Michelle Noonan
    Greenhorn

    Joined: Nov 29, 2008
    Posts: 5
    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?
    Michelle Noonan
    Greenhorn

    Joined: Nov 29, 2008
    Posts: 5
    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.
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    Originally posted by Michelle Noonan:
    ... I deleted the CLASSPATH reference entirely...

    Thank you!

    As Campbell said, there is no end to the confusion caused by CLASSPATH settings. Beginners are best without a system CLASSPATH at all.


    "We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
    sscce.org
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38509
        
      23
    It's working? Well done
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    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 ]
     
    wood burning stoves
     
    subject: Trouble with my Hello World