wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Head First Code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Head First Code" Watch "Head First Code" New topic
Author

Head First Code

Al Wells
Ranch Hand

Joined: Apr 18, 2005
Posts: 87
I am having a problem compiling code copied from Head First Java Edition 2. When I compile the class file that follows, I get the following compiler error:

"Cannot access Exception bad class file .\Exception.java. file does not contain class Exception. Please remove or make sure it appears in the correct subdirectory of the classpath.
catch (Exception ex) {ex.printStackTrace()}"

The code that I copied and tried to run follows. Thank you in advance for your help.

import javax.sound.midi.*;

public class MiniMusicPlayer1 {
public static void main (String [] args) {

try {
Sequencer sequencer = MidiSystem.getSequencer();
sequencer.open();

Sequence seq = new Sequence(Sequence.PPQ, 4);
Track track = seq.createTrack();

for (int i = 5; i < 61; i += 4) {
track.add(makeEvent(144,1,i,100,i));
track.add(makeEvent(128,1,i,100,i + 2));
}

sequencer.setSequencer(seq);
sequencer.setTempoInBPM(220);
sequencer.start();
}
catch (Exception ex) { ex.printStackTrace(); }
}

public static MidiEvent makeEvent (int comd, int chan, int one, int two, int tick) {
MidiEvent Event = null;
try {
ShortMessage a = new ShortMessage();
a.setMessage(comd, chan, one, two);
Event = new MidiEvent(a, tick);

}
catch (Exception e) {}
return Event;
}

}
Al Wells
Ranch Hand

Joined: Apr 18, 2005
Posts: 87
Sorry. Corrected code follows. Still has an Exception problem though:

import javax.sound.midi.*;

public class MiniMusicPlayer1 {
public static void main (String [] args) {

try {
Sequencer sequencer = MidiSystem.getSequencer();
sequencer.open();

Sequence seq = new Sequence(Sequence.PPQ, 4);
Track track = seq.createTrack();

for (int i = 5; i < 61; i += 4) {
track.add(makeEvent(144,1,i,100,i));
track.add(makeEvent(128,1,i,100,i + 2));
}

sequencer.setSequence(seq);
sequencer.setTempoInBPM(220);
sequencer.start();
}
catch (Exception ex) { ex.printStackTrace(); }
}

public static MidiEvent makeEvent (int comd, int chan, int one, int two, int tick) {
MidiEvent Event = null;
try {
ShortMessage a = new ShortMessage();
a.setMessage(comd, chan, one, two);
Event = new MidiEvent(a, tick);

}
catch (Exception e) {}
return Event;
}

}
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
Originally posted by Al Wells:
"Cannot access Exception bad class file .\Exception.java. file does not contain class Exception. Please remove or make sure it appears in the correct subdirectory of the classpath.
catch (Exception ex) {ex.printStackTrace()}"


do you have any files named "Exception.java" or "Exception.class" in the current directory when you're doing this? because you almost certainly shouldn't have. the class Exception is a built-in Java standard one, and you likely do not want to write your own version of it.

if that's not the problem - i.e., if there's no file named "Exception.{java,class}" lying around to confuse your compiler - then it's more likely your Java installation is broken somehow. a correct JRE/JDK should find its standard basic classes on its own without causing errors about it. can you compile anything else with this javac compiler, in any other directory? can you get a "hello world" to run?
Al Wells
Ranch Hand

Joined: Apr 18, 2005
Posts: 87
Everything compiles and runs unless it calls an exception. As soon as I put certain catch blocks in, the compiler throws the error.

It is rather odd as I have not changed any of my directories other than the one where I store class files which is in a directory I created called 'learnjava'. Learnjava is on the root directory. I changed my path directory under Windows so that I can call to it from any directory (several weeks ago) and have not touched any of the Java directories since. I am putting in a new board and processor today and will be formatting the drive. Maybe the reinstall will do the trick. But, it would be nice to know what is going wrong so that I don't face the same issue in the future (or can fix it if I do).

I have searched through the Java directory looking for this file. Any ideas on where in that directory it might be?

Thanks and All the best,
Al
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
in a normal Java installation, the Exception class is defined in some class file which is wrapped up in a .jar package -- you don't normally get to see the class files which make up the bulk of the standard Java API, unless you go to the extra trouble of unpacking those .jar files.

if your compiler is not letting you catch Exception, my first guess would be that your Java install is screwed up somehow. let us know if that reinstall ends up fixing it.
Al Wells
Ranch Hand

Joined: Apr 18, 2005
Posts: 87
Thanks for the help. The re-install did not take care of it. But, based on your advice, I erased every class file in the directory that I created this program in. I had been storing all prior classes in the same directory so that I could refer to them as examples. Once I erased all of them (leaving only the file in question), the compiler was just fine. I must have had SOMETHING in that directory which was conflicting. The compiler certainly did not like it.

Thanks so much.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Head First Code
 
Similar Threads
MiniMusicPlayer2 class in Head First Java book (Chapter 12)
Java Class hangs after run
Program Works, But Why Does It Hang At The End?
MidiEvent Issues.
MiniMusicPlayer HFJ