Excitation of cavity modes in rectangular Josephson junctions