18

I have installed the Eclipse [Version: Photon Release (4.8.0)] and JDK 10 on a MacBookPro with macOS 10.13.5 When I write in my code: import java.awt.*;

I get the error:

The import java.awt cannot be resolved

Is the java.awt included in JDK 10 ? If yes where is and how can I make visible to Eclipse? If no how can I add java.awt?

Naman
  • 27,789
  • 26
  • 218
  • 353
Livio
  • 249
  • 1
  • 2
  • 7
  • 4
    Have you added that your project requires the module for awt? – Luiggi Mendoza Jul 24 '18 at 16:08
  • 5
    From Java 9 onwards, Java is modularized. awt is 99% useless in Java apps and is therefore in a separate module (java.desktop) that is not loaded by default. You must explicitly tell Java to include this module. – Arnaud Denoyelle Jul 24 '18 at 16:11
  • I think the java.awt is included in JDK10, so I add only the line: import java.awt.* in my code. How can I tell to JDK to include java.awt ? – Livio Jul 24 '18 at 16:12
  • 1
    @Livio it is included in JDK10 but is not loaded when starting the JVM or seen by the compiler except if you explicitly ask for it. That's why this package is not found. – Arnaud Denoyelle Jul 24 '18 at 16:16
  • How can I ask to the compiler to load java.awt? Sorry for the obvious question but is the first day I use SDK and Eclipse. Thank you – Livio Jul 24 '18 at 16:21
  • @Livio I don't know (that's why I did not post an answer). But I think that this link is a good start : http://www.baeldung.com/java-9-modularity – Arnaud Denoyelle Jul 24 '18 at 16:25
  • In Eclipse - Package Explorer I can see JRE System Library - java.desktop - java.awt but the error is still there. – Livio Jul 24 '18 at 16:26
  • 2
    @Livio do you use modules? If yes you must declare `requires java.desktop;` inside your modules.java – Lino Jul 24 '18 at 16:39

2 Answers2

49

Is the java.awt included in JDK 10?

Ye, the package does exist. The Java10 API docs do confirm the same as well.

If yes where is and how can I make visible to Eclipse?

In a modular code, all you need to do, is to resolve the java.desktop module by declaring a dependency on it in your module-descriptor file(module-info.java). i.e.

requires java.desktop;
Naman
  • 27,789
  • 26
  • 218
  • 353
3

Basically, just add this maven build compiler plugin portion to your main project Maven POM.xml
to specify which JDK9+ modules MUST BE added for javac compilation purposes.

  <?xml version="1.0" encoding="UTF-8"?>
  <project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    
    ...
    
    <properties>
        <java.version>11</java.version>
        ...
    </properties>

    ...

    <build>
        <plugins>
             <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>

                    <!-- JDK9+ Add Module MAGIC happens below...          -->
                    <!-- Add implicit default JDK9+ java.se explicitly    -->
                    <!-- Add explicit java.desktop for import java.awt.*; -->
                    <compilerArgs>
                        <arg>--add-modules</arg>
                        <arg>java.se,java.desktop</arg>
                    </compilerArgs>
                </configuration>
            </plugin>
        </plugins>
    </build>

  </project>

as explained here:

https://www.baeldung.com/java-9-modularity

For other common missing JDK10+ modules, please refer to:

http://cr.openjdk.java.net/~iris/se/10/pfd/java-se-10-pfd-spec-01/api/overview-summary.html

fprog
  • 41
  • 2