File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes java code not running Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java code not running" Watch "java code not running" New topic
Author

java code not running

sivasubramanian nagarajan
Greenhorn

Joined: Aug 07, 2008
Posts: 11
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

Joined: Aug 04, 2008
Posts: 57
Please post the code also that you are trying to run
sivasubramanian nagarajan
Greenhorn

Joined: Aug 07, 2008
Posts: 11
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

Joined: Aug 01, 2008
Posts: 44
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.


Thanks & Regards
Eshu Khare
Shahnawaz Shakil
Ranch Hand

Joined: Aug 04, 2008
Posts: 57
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

Joined: Jul 26, 2007
Posts: 10246
    
    8

...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.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

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.


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Aug 05, 2005
Posts: 3446
    
  12
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.


Joanne
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10246
    
    8

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

Joined: Aug 03, 2008
Posts: 9293
    
  17

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

Joined: Jul 26, 2007
Posts: 10246
    
    8

Originally posted by Ankit Garg:

Well boy in the world...


Long time any one called me a boy!
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10246
    
    8

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

Joined: Aug 01, 2008
Posts: 44
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

Joined: Aug 03, 2008
Posts: 9293
    
  17

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

Joined: Jul 26, 2007
Posts: 10246
    
    8

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

Joined: Aug 03, 2008
Posts: 9293
    
  17

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

Joined: Aug 07, 2008
Posts: 11
thanks all i finally got it working.....
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
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

Joined: Oct 13, 2005
Posts: 38340
    
  23
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java code not running