aspose file tools*
The moose likes Beginning Java and the fly likes My first java program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "My first java program" Watch "My first java program" New topic
Author

My first java program

Amulya Green
Greenhorn

Joined: Dec 28, 2005
Posts: 19
Hi
I just started reading head first java book and i tried to compile the following program.

public class myfirstapp
{
public static void main (String [] args)
{
System.out.println("Hello World");
}
}
this program compiles.but it is throwing me the following execption when i run it
Exception in thread "main" java.lang.NoClassDefFoundError: myfirstapp

Can somebody please tell me how to get rid of it.
Thanks
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
First I am going to assume that you are running your programs on Windows using the command-line. If not, please tell me what operating system and IDE you are using. So let's say that you save your program in C:\myjava\myfirstapp.java. Then you should open a command prompt and type the following commands to compile and run it:

Since you say that you compiled it, I assume that you did the first two steps just fine. One common error is that you forget to change to the correct directory, so double check this.

The other potential problem is if you set the CLASSPATH variable or if you have other programs installed that use it. To find out, you can type the following command at the command-prompt:

It will help if you copy and paste the output from this command for us so we can see what it says. Most likely, you should remove the CLASSPATH variable from your system. At this point it is not needed to write your own programs. If you are sure that it won't break any other software on your system, then I suggest that you remove it. If you don't know how, then please come back and we will help you further.

Layne


Java API Documentation
The Java Tutorial
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13868
    
  10

Have a look at the following tutorial page, it explains step by step what you have to do to write, compile and run your very first Java application, and it explains all the possible errors you could encounter (including the one you are getting now) and what you should do about them:

The "Hello World" Application


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Amulya Green
Greenhorn

Joined: Dec 28, 2005
Posts: 19
thank you for ur reply. when i typed echo at the command prompt, this is what i got.

C:\Documents and Settings\Owner>echo %classpath%
C:\Program Files\IBM\WebSphere MQ\Java\lib\providerutil.jar;C:\Program Files\IBM
\WebSphere MQ\Java\lib\com.ibm.mqjms.jar;C:\Program Files\IBM\WebSphere MQ\Java\
lib\ldap.jar;C:\Program Files\IBM\WebSphere MQ\Java\lib\jta.jar;C:\Program Files
\IBM\WebSphere MQ\Java\lib\jndi.jar;C:\Program Files\IBM\WebSphere MQ\Java\lib\j
ms.jar;C:\Program Files\IBM\WebSphere MQ\Java\lib\connector.jar;C:\Program Files
\IBM\WebSphere MQ\Java\lib\fscontext.jar;C:\Program Files\IBM\WebSphere MQ\Java\
lib\com.ibm.mq.jar

thank you once again for ur time
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Try this

javac -classpath . myfirstapp
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
and

java -classpath . myfirstapp should interpret the program.
Amulya Green
Greenhorn

Joined: Dec 28, 2005
Posts: 19
it did not work.thanks for trying
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Amulya Green:
it did not work.thanks for trying


Unfortunately, you have not provided enough detail for us to help you further. What happened when you tried the suggestion above?

Layne
Amulya Green
Greenhorn

Joined: Dec 28, 2005
Posts: 19
Thank you everybody for putting your time in.
I got it finally.The "java filename" command was not looking in the right place for the class file.So i had to set the class path and i got it.
Thanks
Amulya
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: My first java program
 
Similar Threads
Netbeans and java source files
just started
just getting started. can compile, won't run
Really Frustrated with "main" error message
First dumb question