• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Head First Code

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}

}
 
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic