• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java code not running

 
sivasubramanian nagarajan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
i am new to java and studying from the learners guide by kathy sierra and bert bates. I typed a code from the book, it compiled successfully but did not run and got the following errors. please help me in removing these errors

Exception in thread "main" java.lang.NoClassDefFoundError: firstprg
Caused by: java.lang.ClassNotFoundException: firstprg
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
 
Shahnawaz Shakil
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post the code also that you are trying to run
 
sivasubramanian nagarajan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shahnawaz Shakil:
Please post the code also that you are trying to run


class DVDPlayer{

boolean player=true;

void player(){
System.out.println("DVD is playing");
}

void playerRecord(){
System.out.println("DVD is recording");
}
}

class DVDPlayerTestDrive{

public static void main(String[] args){

DVDPlayer d = new DVDPlayer();
d.player();
d.player=false;
d.playerRecord();
while(d.player){
d.playerRecord();
System.out.println("You cannot play dvd as it is recording");
d.player=true;
}
System.out.println("You may start playing");
}
}
 
eshu khare
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the solution:-


One more thing to note down is that here your compilation unit has got to be DVDPlayerTestDrive not the DVDPlayer.And then it not only get compiled successfully but also it runs,Lookin for some alternate solution because this does not seems to be in very proper format.
 
Shahnawaz Shakil
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi eshu khare,
I don't see any difference between your code and that of nagarajan. By the way why you need main in the class DVDPlayer? Infact the code posted by nagarajan is absolutely fine. The only thing he needs to care about is that the file name should be DVDPlayerTestDrive.java
 
Maneesh Godbole
Saloon Keeper
Posts: 11196
15
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...it compiled successfully...
Exception in thread "main" java.lang.NoClassDefFoundError: firstprg
Caused by: java.lang.ClassNotFoundException: firstprg


Just use java DVDPlayerTestDrive instead of java firstprg and it will start running.
 
Ankit Garg
Sheriff
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Maneesh Godbole:


Just use java DVDPlayerTestDrive instead of java firstprg and it will start running.


also remember that the classpath environment variable must have a . in it.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sivasubramanian nagarajan:

Exception in thread "main" java.lang.NoClassDefFoundError: firstprg


you have anything called firstprg?... before that make sure that you set the classpath.

Hope This Helps
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shahnawaz Shakil:
The only thing he needs to care about is that the file name should be DVDPlayerTestDrive.java


He doesn't even need to worry about that as his class is not public.
 
Maneesh Godbole
Saloon Keeper
Posts: 11196
15
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ankit Garg:


also remember that the classpath environment variable must have a . in it.


Not really.
If you are executing the "java" command from the directory where the .class files are located, you dont need to include the current directory in the class path
 
Ankit Garg
Sheriff
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Maneesh Godbole:


Not really.
If you are executing the "java" command from the directory where the .class files are located, you dont need to include the current directory in the class path


Well boy in the world in which I live you have to include . in the classpath even if you are executing the program from the same directory where the .class file is. Which OS are you talking about. You have to include . in the classpath in both Windows and Linux...
 
Maneesh Godbole
Saloon Keeper
Posts: 11196
15
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ankit Garg:

Well boy in the world...


Long time any one called me a boy!
 
Maneesh Godbole
Saloon Keeper
Posts: 11196
15
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok.
Enough of the nonsense.

This is from my world as you call it taken from http://java.sun.com/j2se/1.3/docs/tooldocs/win32/classpath.html
The default class path is the current directory. Setting the CLASSPATH variable or using the -classpath command-line option overrides that default, so if you want to include the current directory in the search path, you must include "." in the new settings.


What it essentially means is that if you have NOT set the CLASSPATH environment variable AND are NOT using the -classpath command line option, then your current directory IS on the classpath.

You might also find this link useful.
 
eshu khare
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shahnawaz Shakil
greenhorn
Member # 178390
posted Today 2:20 PM
--------------------------------------------------------------------------------
Hi eshu khare,
I don't see any difference between your code and that of nagarajan. By the way why you need main in the class DVDPlayer? Infact the code posted by nagarajan is absolutely fine. The only thing he needs to care about is that the file name should be DVDPlayerTestDrive.java

Hi Shahnawaz Shakil,
You have not seen the code properly there is main below both the defined classes in answere,and you can find the difference by compiling both the code,the basic solution I stated already that compilation unit has got to be DVDPlayerTestDrive.Hope this will clear your doubt.
 
Ankit Garg
Sheriff
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Maneesh Godbole:

What it essentially means is that if you have NOT set the CLASSPATH environment variable AND are NOT using the -classpath command line option, then your current directory IS on the classpath.



I messed. You are right. I hope you will not mind me calling you a boy while now I feel like I am a new born and you have been in this world for past 200years......
 
Maneesh Godbole
Saloon Keeper
Posts: 11196
15
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ankit Garg:
I hope you will not mind me calling you a boy while now I feel like I am a new born and you have been in this world for past 200years......


Hey! Now you are calling me an old geezer!
Jokes apart, never forget the important java ranch rule...be nice.
 
Ankit Garg
Sheriff
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Maneesh Godbole:


Hey! Now you are calling me an old geezer!
Jokes apart, never forget the important java ranch rule...be nice.


I was not offencing you. I was just feeling sorry for commenting on something which I don't have complete knowledge of...

Neither I am an infant nor you are an Old GEEZER!!! OK!!!
 
sivasubramanian nagarajan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks all i finally got it working.....
 
Campbell Ritchie
Sheriff
Pie
Posts: 50289
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sivasubramanian nagarajan:
thanks all i finally got it working.....
Well done; please tell us what you did so others can learn from it.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50289
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit and Maneesh, I can see from your posts that neither of you intended to cause offence ( ), but you need to be very careful what you say on the net. You can't see the person you are speaking to, you can't see their reactions, and you can't reply immediately to say it was just a joke.

I am sure neither of you has taken offence, but this thread shows how you have to strive to be polite at all times because a forum works "at a remove." It is perfectly possible for an innocent remark to be taken the wrong way because you are writing "at a remove" and at a distance. That is what we mean on JavaRanch by "Be Nice."
[ August 11, 2008: Message edited by: Campbell Ritchie ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic