Search code examples
javaeclipseimport

java eclipse import errors referring to swing, awt


I'm new to Eclpse (just downloaded this: Eclipse IDE for Java Developers Version: 2020-03 (4.15.0) Build id: 20200313-1211 OS: Linux, v.4.15.0-91-generic, x86_64 / gtk 3.22.30, WebKit 2.28.1 Java version: 11.0.7). I already had javac, which reports the same 11.0.7.

I have a single .java file, about 5,000 lines, that I developed without using Eclipse. It compiles fine with javac, using javac CharManager.java as the full command line. As I'm not experienced with java, I'm sure it's stylistically awful, but javac gives no warnings. It contains no module declarations (barely know what those are), lots of classes, and lots and lots of import statements. Since it compiles fine, I assumed it wouldn't be a problem when moved to an Eclipse project. I was wrong.

Eclipse is flagging many of my import statements, basically anything mentioning .awt, .swing or .swingx. javac is happy, so what's Eclipse's problem? I don't see anything to click on to say, "Hey, this is a GUI app, I expect these dozens of GUI classes to be available." It causes all reference to things like Color or JPanel to be unrecognized, so of course I can't compile.

Based on other hints I tried setting the mysterious module-info.java to

module CharManager {
    requires javax.swing.event.ChangeListener;
}

but now it's unhappy with that line, too.

Basically I'm stymied. At some level I think I'm stunned by the fact that an IDE has a problem with code a compiler is fine with. I didn't need a module-info.java before and don't see what it's contributing now (except errors). All I really want to do is debug my perfectly compilable and runnable code. With the understanding that I know nothing about Eclipse, what am I missing?


Solution

  • Eclipse defaults to creating a module-info.java files when you create a new Java project (this can be changed on the second page of the New Java Project wizard). Creating this file makes the project use Java modules.

    If the module-info.java was created in error you can just delete it to revert to not using modules.

    If the module info file exists you need to specify the modules you are using. The Swing code is in the java.desktop module:

    module CharManager {
        requires java.desktop;
    }