We have developed a program which computes the resonant frequencies and fields in an arbitrary shaped three dimensional cavity by finite element method. We take electric field E = (Ex, Ey, Ez) as unknown variables and solve the eigenvalue problem for the Maxwell equations in time-harmonic case imposing boundary conditions. Divergence-free condition is treated approximately by penalty method. We show the formulation for three dimensional analysis of RF electromagnetic fields and some test calculations. Satisfactory results are obtained for rectangular cavities, cylindrical cavities and those with smoothly deformed parts. In the present calculations, number of the nodal points is limited to less than 1000 because of the memory size of the computer, which means about ten nodes in each direction. For very complicated boundary shape, errors become large because mesh size is coarse and therefore the condition div E = 0 is not fully satisfied. But this difficulty will be overcome with increasing available memory size.