Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 505
J. Doe Avatar asked Oct 28 '18 16:10

J. Doe


People also ask

How do you fix must declare a named package in Eclipse?

To Solve must declare a named package eclipse because this compilation unit is associated to the named module x Error Package name left blank while creating a class. This make use of default package. Thus causes this error. So You just need to delete module-info.

What is a named package?

A package name is the full name of the package, which is defined via the NAME parameter in the pkginfo file. Because system administrators often use package names to determine whether or not a package needs to be installed, it is important to write clear, concise, and complete package names.

How do I find the package name in Eclipse?

Click on the root package and start typing the name of the package you are looking for. Show activity on this post. Ctrl + Shift + T works for packages too: Just type the package name and then a dot and it will show all classes in matching packages.


2 Answers

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

like image 162
Danh Tran Avatar answered Nov 08 '22 09:11

Danh Tran


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
like image 28
reben Avatar answered Nov 08 '22 10:11

reben