39

I have started learning Java and encountered a problem when trying to run my first program as given below:

public class HelloWorld {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
     System.out.println("Hello world!");
    }
}

On Eclipse Photon I encounter this error when running it:

 Error occurred during initialization of boot layer
    java.lang.module.FindException: Error reading module: C:\Users\Thomas\eclipse-workspace\HelloWorld\bin
    Caused by: java.lang.module.InvalidModuleDescriptorException: HelloWorld.class found in top-level directory (unnamed package not allowed in module)

I looked and there is my .class file in bin directory and my .java in the src directory.

Is that normal? How do I fix that?

Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42
Hainan_dev
  • 391
  • 1
  • 3
  • 6
  • Looks like Jave 9. You have to put the class in a package; – jr593 Jul 02 '18 at 10:01
  • 1
    Add the following statement as first line in your code: `package example;`Eclipse will show you an error. Use the _Quick Help_ and choose "Move ..." – LuCio Jul 02 '18 at 10:14

8 Answers8

79

I was getting the same error. Deleting the module-info.java file solved it for me.

Jaideep Dhumal
  • 903
  • 6
  • 6
26

It seems that you haven't created a package. My usual procedure in Eclipse is:

  • Create a new Java project
  • Inside that project: Create a new package
  • Inside that package: Create a new Java class

Eclipse will help you a lot with the settings. Then just copy your code into that class and hit the 'start' button.

Naman
  • 27,789
  • 26
  • 218
  • 353
Sven Affeld
  • 311
  • 3
  • 8
6

by removing module class problem solved for me in eclipse

Naveen Neelam
  • 69
  • 1
  • 2
0

i moved my main class and sample.fxml file to a new package that throws this error below

"Error occurred during initialization of boot layer java.lang.module.FindException: Error reading module: C:\Users\Thomas\eclipse-workspace\HelloWorld\bin Caused by: java.lang.module.InvalidModuleDescriptorException: HelloWorld.class found in top-level directory (unnamed package not allowed in module)"

i moved my class file and fxml file to normal position. it fix my problem. but i have to find why it happends. cheers....

0

Executing the project from another workspace solved for me in Eclipse.

0

create a package and refactor your Main class in that package in your case its HelloWorld then it will run fine. To understand how modules work in java please watch this spectacular video MODULES IN JAVA

Nabeel Bhatti
  • 99
  • 2
  • 10
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 09 '22 at 11:30
0

I've solved it according to these steps:

  1. right click on your main project name
  2. choose the new package and fill in the name
  3. choose (radio button) "create package info"
  4. click ok
  5. finally, in the package explorer, drag your class under the new package which you created
Arman
  • 126
  • 3
  • 14
NIMA
  • 21
  • 1
  • 1
  • 3
0

Delete module-info.java from your project and run ,it should work. Or Create package and then create class inside package.

Ashish Gupta
  • 105
  • 1
  • 5