Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can compile java code but not run (exception)

 
Daniel Lucas
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, this is the first program I've tried to run on my pc at home. I've gone through a year of programming at high school and learned very little, so I'm doing Head First Java on my own at home. At school I used TextPad to compile and run the code, which is what I'm doing at home for now (eventually I'll move on to use eclipse, methinks). Anyway, I installed the j2se Software Development Kit 1.5 (5.0) Update 6. I hate it how sun puts the Runtime Environment so close to the SDK... ugh! But anyway, I managed to avoid downloading the wrong one.

TextPad compiles the following code correctly:



But when I try to run I get this exception:
What's the problem? I read a previous thread with 30 posts about setting the PATH. Is that my problem here?
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No the PATH is not your problem, it's the CLASSPATH. You need to make sure that the .class file can be found in the CLASSPATH.
 
Daniel Lucas
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my system variables I have a variable called CLASSPATH with the value "C:\Program Files\Java\jre1.5.0_06\lib\ext\QTJava.zip"

That's for the Java Runtime Environment. What do I need to do with the CLASSPATH to get the program to run properly?
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way is to modify the System CLASSPATH and include . for the current directory.

Another option is to set the CLASSPATH on the command line.





You can include multiple directories in the CLASSPATH by seperating them with a semicolon.
 
Daniel Lucas
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NEVERMIND GUYS! I just got it to work. What's confusing is the installation instructions sun gives ( http://java.sun.com/j2se/1.5.0/install-windows.html ) does not mention the need to add both a PATH AND CLASSPATH to the User Variables. Now I can use TextPad just like at school!
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because normally you do not have to set the CLASSPATH environment variable. By default, if CLASSPATH isn't set, Java looks in the current directory for class files.

You have Apple QuickTime installed on your computer, which set the CLASSPATH variable. Now Java looks ONLY at the QuickTime ZIP file, because the current directory (".") isn't in your classpath.

So it's not Sun's fault, it's Apple's QuickTime that messed up your CLASSPATH setting.
 
Daniel Lucas
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't realize that Quicktime did that until you just told me, Jesper Young. I guess it was luck that I managed to solve my problem (yes I did have Quicktime installed).

On the other hand, it is Sun's fault and it isn't Sun's fault that Quicktime screws up their SDK. It isn't Sun's fault because someone else's software conflicts with Sun's, while it is Sun's fault because they did not mention in the installation instructions that this is a common problem and how to fix it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic