Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

module-info.java does not work with lombok in Java 9

I faced problem during migration our project to Java 9.

After I've updated Java 9, I attempt to run project, I faced with compiler errors :-

Error:(6, 1) java: package javax.annotation is not visible 
(package javax.annotation is declared in module java.xml.ws.annotation, which is not in the module graph) 

but I found the solution how to resolve it. I added lombok.config file.

Then after adding module-info.java file to project compiler is displayed errors again

Error:(10, 26) java: variable title not initialized in the default constructor

Project example:

We have entity Store:

@AllArgsConstructor
@Getter
public class Story {
    private final String title;
}

in root's package I have module-info.java with content:

module javanine {
    requires lombok;
}

and in root's project I have lombok.config file with:

lombok.addJavaxGeneratedAnnotation = false
lombok.anyConstructor.suppressConstructorProperties = true

config.stopBubbling = true

and somewhere in the code I call it :

public static void main(String[] args) {
    Story story = new Story("how as");
    System.out.println(story.getTitle());
}
like image 544
Andrey Avatar asked Nov 23 '17 16:11

Andrey


People also ask

Does Lombok work with Java 11?

Latest version of Lombok and/or IntelliJ plugin perfectly supports Java 11.

Does Lombok work with Java 6?

Pretty sure Lombok supports JDK 6+, so there is no reason for you to change version.

Why you should not use Lombok?

The downside is it clutters your source with boilerplate code that take the focus off the important stuff. After 5 months, we're still using Lombok, but I have some other annoyances. The lack of a declared getter & setter can get annoying at times when you are trying to familiarize yourself with new code.

Can we use Lombok in simple Java project?

The project Lombok is a popular and widely used Java library that is used to minimize or remove the boilerplate code. It saves time and effort. Just by using the annotations, we can save space and readability of the source code. It is automatically plugging into IDEs and build tools to spice up our Java application.

What is Java 9 modules?

It is also known as Project Jigsaw. In this Java 9 modules example, we will learn about modules (in general) and how your programming style will change in the future when you start writing modular code. 1. What is a Module?

How do I use a module in Java?

A module is typically just a jar file that has a module-info.class file at the root. To use a module, include the jar file into modulepath instead of the classpath. A modular jar file added to classpath is normal jar file and module-info.class file will be ignored.

Is it dependent on any other modules in Java?

It’s an Independent module and does NOT dependent on any other modules. By default, all other modules are dependent on “ java.base “. In Java 9, modules help us in encapsulating packages and managing dependencies.

What is the difference between a class and a module in Java?

By default, all other modules dependent on “ java.base ”. In java 9, modules helps you in encapsulating packages and manage dependencies. So typically, a class is a container of fields and methods. a package is a container of classes and interfaces. a module is a container of packages.


1 Answers

Just configure your module as following:

module moduleName {
    requires static lombok;
}

like image 146
Eugene Ryzhikov Avatar answered Oct 02 '22 16:10

Eugene Ryzhikov