'Unresolved compilation problem exception using JOptionPane

I am a beginner in Java and I am trying to use JOptionPane to display a message box but keep getting stuck with an error which I cannot figure out. This is my code:

package excercise1; 
import javax.swing.JOptionPane;

public class Part3 {

    public static void main(String[] args) {
        int testVal = 20;
        JOptionPane.showMessageDialog(null, "The number is " + testVal);
    }

}

I keep getting this error:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: JOptionPane cannot be resolved

at excercise1/excercise1.Part3.main(Part3.java:8)

Does anyone know how I can fix this? Thanks!

EDIT: I am running this code in Eclipse by just clicking on the run button in the top toolbar, if I should be running this code differently please let me know.

EDIT #2: Screenshot of my Eclipse screen: enter image description here



Solution 1:[1]

Try removing the JRE system library and adding it again:

Go to properties of project with the build error (right click > Properties)

View the "Libraries" tab in the "Build Path" section

Find the "JRE System Library" in the list (if this is missing then this error message is not an eclipse bug but a mis-configured project)

Remove the "JRE System Library"

Hit "Add Library ...", Select "JRE System Library" and add the appropriate JRE for the project (eg. 'Workspace default JRE')

Hit "Finish" in the library selection and "OK" in the project properties and then wait for the re-build of the project

Solution 2:[2]

I had the same problem, I know it's late but it can help others... Try checking if the module-info.java exists and if it is in the same package as your class. This will probably solve your problem.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 algorythms
Solution 2 Volodya Lombrozo