[java] must declare a named package eclipse because this compilation unit is associated to the named module

I just downloaded eclipse for Java Yesterday but when I was trying to get make my first program, I kept on getting this error:

must declare a named package eclipse because this compilation unit is associated to the named module x. 

How do I fix this?

This question is related to java error-handling

The answer is


Just delete module-info.java at your Project Explorer tab.


The "delete module-info.java at your Project Explorer tab" answer is the easiest and most straightforward answer, but

for those who would want a little more understanding or control of what's happening, the following alternate methods may be desirable;

  • make an ever so slightly more realistic application; com.YourCompany.etc or just com.HelloWorld (Project name: com.HelloWorld and class name: HelloWorld)

or

  • when creating the java project; when in the Create Java Project dialog, don't choose Finish but Next, and deselect Create module-info.java file

Reason of the error: Package name left blank while creating a class. This make use of default package. Thus causes this error.

Quick fix:

  1. Create a package eg. helloWorld inside the src folder.
  2. Move helloWorld.java file in that package. Just drag and drop on the package. Error should disappear.

Explanation:

  • My Eclipse version: 2020-09 (4.17.0)
  • My Java version: Java 15, 2020-09-15

Latest version of Eclipse required java11 or above. The module feature is introduced in java9 and onward. It was proposed in 2005 for Java7 but later suspended. Java is object oriented based. And module is the moduler approach which can be seen in language like C. It was harder to implement it, due to which it took long time for the release. Source: Understanding Java 9 Modules

When you create a new project in Eclipse then by default module feature is selected. And in Eclipse-2020-09-R, a pop-up appears which ask for creation of module-info.java file. If you select don't create then module-info.java will not create and your project will free from this issue.

Best practice is while crating project, after giving project name. Click on next button instead of finish. On next page at the bottom it ask for creation of module-info.java file. Select or deselect as per need.

If selected: (by default) click on finish button and give name for module. Now while creating a class don't forget to give package name. Whenever you create a class just give package name. Any name, just don't left it blank.

If deselect: No issue


Questions with java tag:

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error Instantiating a generic type When to create variables (memory management) java doesn't run if structure inside of onclick listener String method cannot be found in a main class method Are all Spring Framework Java Configuration injection examples buggy? Calling another method java GUI I need to know how to get my program to output the word i typed in and also the new rearranged word using a 2D array Java and unlimited decimal places? Read input from a JOptionPane.showInputDialog box Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable Two Page Login with Spring Security 3.2.x Hadoop MapReduce: Strange Result when Storing Previous Value in Memory in a Reduce Class (Java) Got a NumberFormatException while trying to parse a text file for objects Best way for storing Java application name and version properties Call japplet from jframe FragmentActivity to Fragment Comparing two joda DateTime instances Maven dependencies are failing with a 501 error IntelliJ: Error:java: error: release version 5 not supported Has been compiled by a more recent version of the Java Runtime (class file version 57.0) Why am I getting Unknown error in line 1 of pom.xml? Gradle: Could not determine java version from '11.0.2' Error: Java: invalid target release: 11 - IntelliJ IDEA Android Gradle 5.0 Update:Cause: org.jetbrains.plugins.gradle.tooling.util Why is 2 * (i * i) faster than 2 * i * i in Java? must declare a named package eclipse because this compilation unit is associated to the named module How do I install Java on Mac OSX allowing version switching? How to install JDK 11 under Ubuntu? Java 11 package javax.xml.bind does not exist IntelliJ can't recognize JavaFX 11 with OpenJDK 11 Difference between OpenJDK and Adoptium/AdoptOpenJDK OpenJDK8 for windows How to allow all Network connection types HTTP and HTTPS in Android (9) Pie? Find the smallest positive integer that does not occur in a given sequence Error: JavaFX runtime components are missing, and are required to run this application with JDK 11 How to uninstall Eclipse? Failed to resolve: com.google.firebase:firebase-core:16.0.1 How to resolve Unable to load authentication plugin 'caching_sha2_password' issue

Questions with error-handling tag:

must declare a named package eclipse because this compilation unit is associated to the named module Error:Failed to open zip file. Gradle's dependency cache may be corrupt What does 'index 0 is out of bounds for axis 0 with size 0' mean? What's the source of Error: getaddrinfo EAI_AGAIN? Error handling with try and catch in Laravel What does "Fatal error: Unexpectedly found nil while unwrapping an Optional value" mean? Raise error in a Bash script Javascript Uncaught TypeError: Cannot read property '0' of undefined Multiple values in single-value context IndexError: too many indices for array REST API error code 500 handling How do I debug "Error: spawn ENOENT" on node.js? How to check the exit status using an if statement Getting net::ERR_UNKNOWN_URL_SCHEME while calling telephone number from HTML page in Android How to handle ETIMEDOUT error? Is there a TRY CATCH command in Bash #1064 -You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version Why is "except: pass" a bad programming practice? How can I solve the error LNK2019: unresolved external symbol - function? 400 BAD request HTTP error code meaning? Eclipse returns error message "Java was started but returned exit code = 1" live output from subprocess command Is it not possible to stringify an Error using JSON.stringify? PHP Notice: Undefined offset: 1 with array when reading data How to get error message when ifstream open fails Error:attempt to apply non-function Notice: Undefined variable: _SESSION in "" on line 9 ErrorActionPreference and ErrorAction SilentlyContinue for Get-PSSessionConfiguration How to capture no file for fs.readFileSync()? Reference - What does this error mean in PHP? Handling a timeout error in python sockets Disabling Strict Standards in PHP 5.4 When should I use Write-Error vs. Throw? Terminating vs. non-terminating errors What is the difference between `throw new Error` and `throw someObject`? Powershell: How can I stop errors from being displayed in a script? Does Python have an argc argument? vba error handling in loop php return 500 error but no error log Error: could not find function ... in R How to catch integer(0)? Enabling error display in PHP via htaccess only Fastest way to check if a string is JSON in PHP? Deploying website: 500 - Internal server error How to fix Error: "Could not find schema information for the attribute/element" by creating schema Warning: implode() [function.implode]: Invalid arguments passed Where does PHP store the error log? (php5, apache, fastcgi, cpanel) In Python try until no error Raise warning in Python without interrupting program How do I log errors and warnings into a file? How to do error logging in CodeIgniter (PHP)