0

I could not get my code to compile, I even copied the code from the github here it is

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;
    }
}

I type in javac MiniMusicPlayer1.java and it gives me a number of errors, all related to midievent. The first error says Midievent.java:1 error: class...expected.

The main error I see is

"cannot access Midievent...bad source file .\MidiEvent.java.....file does not contain class MidiEvent....Please remove or make sure it appears in the correct subdirectory of the sourcepath

What could the problem be? I read other people using the same code without issues

Pshemo
  • 122,468
  • 25
  • 185
  • 269
gallly
  • 1,201
  • 4
  • 27
  • 45
  • This might be helpful: http://stackoverflow.com/questions/12117680/java-package-does-not-exist-and-bad-source-file – Andrew Oct 18 '13 at 21:59
  • 2
    omg I found out my own answer..sorry everyone...It turns out I actually had another class named MidiEvent in the folder ><!!!!thanks for that link though I still learned something helpful lol. – gallly Oct 18 '13 at 22:01
  • If you want to be hardcore and do everything from the command line, that's an extremely useful link. I'd use an IDE though. – Andrew Oct 18 '13 at 22:03

2 Answers2

1

Unless you have the class MidiEvent defined elsewhere, then this code will not compile, as it requires the MidiEvent class.

Based on the error message, it looks like you have a MidiEvent.java file, however the java compiler is not able to find the class MidiEvent in it, either because the file is malformed, or the file is incorrectly named.

Paul Wagland
  • 27,756
  • 10
  • 52
  • 74
1

Make sure the capitalization of the name other source file (MidiEvent.java) and the capitalization of the name of the class inside it (Midievent, seemingly) are the same.

Boann
  • 48,794
  • 16
  • 117
  • 146