Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

just started

 
nicholas lee
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm a novice here. I just purchased Head First Java. I'm about 10 pages deep. I wrote the following

public class myfirstapp {

public static void main (String[] args) {
System.out.println("I rule!");
System.out.println("the world");
}
}

complied it and try to run the compiled file, but was returned this exception in thread "main" java.lang.NoClassDefFoundError: myfirstapp/class???


Please help me feel no so stupid.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

When you compile your program, you include the .java extension...

But when you run your program, do not include the .class extension. Just type...

Let us know if this clears it up.
[ March 29, 2006: Message edited by: marc weber ]
 
nicholas lee
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That didn't work. I still recieve the error

exception in thread "main" java.lang.NoClassDefFoundErorr: myfirstapp


Is there a better interface than using a c dos shell to run java from?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you should read this first:
http://faq.javaranch.com/view?HowToSetTheClasspath and add the current directory to your CLASSPATH.

This has no relation but try to name your classes with capital letters. Like MyFirstApp.java
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Satou kurinosuke:
Maybe you should read this first:
http://faq.javaranch.com/view?HowToSetTheClasspath and add the current directory to your CLASSPATH.


An alternate to setting that operating system environment variable is to use the cp (classpath) command line option:

java -cp . MyFirstApp

This causes the JVM to look in the current directory for class files.
[ March 30, 2006: Message edited by: Jeff Albertson ]
 
ak pillai
author
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a simple observation that, your class name should start with uppercase.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by nicholas lee:
...I still recieve the error

exception in thread "main" java.lang.NoClassDefFoundErorr: myfirstapp

This is a different error than the first (which ended in "/class"). Check to make sure that:
  • You first changed to the directory that contians the .java and .class files (using the command cd followed by the directory -- for example: cd c:\myJava\tests), and...
  • A compiled .class file is actually in that directory (i.e., it compiled successfully), and...
  • The name of your file matches the exact case you are typing (for example, if your file is myFirstApp, then myfirstapp won't work).
  • Note: You should not have any need for a CLASSPATH variable at this point. Your PATH variable seems to be correct, because javac and java are recognized (giving you the errors).

    Getting your system set up is one of the more difficult hurdles in beginning Java. But once you get past this, I think you'll find that the Command Prompt is your best bet for now.
     
    nicholas lee
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you all for you help. Setting the class path work out. I see now that the error registering was just letting me know that java could not find the class I defined.


    Is there documentation I could find which would tell me more about the error messages java return?

    hopefully in a few weeks, I'll be the one responding instead of posting.

    thanks
     
    Don't get me started about those stupid light bulbs.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic