Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The import java.awt cannot be resolved

Tags:

java

java-10

awt

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?

like image 387
Livio Avatar asked Jul 24 '18 16:07

Livio


People also ask

What is import Java awt?

import java. awt. Graphics means that the Graphics class in the java. awt package is made known to the current class.

What is import awt event?

java.awt.event. Provides interfaces and classes for dealing with different types of events fired by AWT components. javax.accessibility. Defines a contract between user-interface components and an assistive technology that provides access to those components.

What is awt API in Java?

Abstract Window Toolkit (AWT) is a set of application program interfaces ( API s) used by Java programmers to create graphical user interface ( GUI ) objects, such as buttons, scroll bars, and windows. AWT is part of the Java Foundation Classes ( JFC ) from Sun Microsystems, the company that originated Java.


2 Answers

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;
like image 60
Naman Avatar answered Sep 21 '22 08:09

Naman


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

like image 27
fprog Avatar answered Sep 20 '22 08:09

fprog