Site icon TestingDocs.com

How to fix InvalidModuleDescriptorException

Introduction

The exception java.lang.module.InvalidModuleDescriptorException is caused by an unnamed package not allowed in a module.

Error

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

 

Fixes

Check the JDK version of the project. InvalidModuleDescriptorException is a runtime exception usually thrown when reading module descriptor.

# Refactor the code

If we want the project to be as Java module, then we need to refactor the code. The above error occurs when Java classes are created right under the default src default package folder. The unnamed package is not allowed in the module with the latest JDK.

We must declare a named package because this compilation unit is associated to the named module.The name of the module can be found in module-info.java during the project creation phase. To avoid this kind of exception, move all the classes that are in the default package to some named packages.

Create a Package.

Steps to create a new package in Eclipse IDE

https://www.testingdocs.com/questions/how-to-create-a-package-in-eclipse-ide/

Refactor the code to move the classes in unnamed packages to named ones.

 

 

# Module descriptor file

If you still see the error:

Check the module descriptor file module-info.java . This file contains the metadata for the module dependencies, packages exports, etc.

# Not a Java Module

If we don’t want the project to be a Java module then simply delete the module-info.java module descriptor file to fix this error.

Java Tutorial on this website: https://www.testingdocs.com/java-tutorial/

For more information on Java, visit the official website :

https://www.oracle.com/in/java/

Exit mobile version