Run Java Code without a package in Eclipse IDE

8.6k views Asked by At

I want to run my code without creating a package in java eclipse IDE.

But when I do so it is showing error :

Must declare a named package because this compilation unit is associated to the named module 'Games'
5

There are 5 answers

0
howlger On BEST ANSWER

You are using the Java Platform Module System (JPMS) which requires not to use the default/unnamed package.

In the default package delete the file module-info.java to not use JPMS and to be able to have code in the default package.

1
Naeem Khan On

Just right click on the project folder -> New -> Package. Once the package is made, move all your classes into it.

8
Vishnu Vijaykumar On

Double click on the error icon at the line number ->move to default package option.

Make sure the classes are (public or protected) to be used in different packages.

0
KevinO On

In the file in question, right click to get the context menu. Select "Refactor->Move". The resultant dialog will have the (default package) into which the source file may be placed.

Also, be sure to not have a package definition line at the start of the source file.

Please note that the use of the default package is generally discouraged, but there are time I still use it (primarily for printing copyright/version information).

enter image description here

enter image description here

There are also options for turning on hidden/suppressed parent packages, but I think the Refactor approach is the most clean.

0
Mudit Chauhan On

The most viable answer to this problem is to remove the module-info.java file located on the Project Explorer tab of Eclipse. This file is created by default. Also, deleting this will not affect your program. Hope this will help.