Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

My first java program

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Amulya Green
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this

javac -classpath . myfirstapp
 
Keith Lynn
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and

java -classpath . myfirstapp should interpret the program.
 
Amulya Green
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it did not work.thanks for trying
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic