Module dependency - package does not exist in compile time - java

I've read a lot of questions & answers in SO, without any luck. Example: Module packages not found at compile time in IntelliJ
The thing is that I've a project which formed by:
Module A
Module B
Module C
Module B has a dependency with module A and module C.
So the imports in module B, to code are like
import com.moduleA.Fragment1;
import com.moduleA.Fragment2;
import com.moduleA.SomeInterface;
Those lines are correctly imported in "coding-time", I can work with those classes correctly.
However when I do compile, it crashes with a:
Error:(8, 32) error: com.moduleA does not exist
I've tried adding this module A as Android Library but it's not OK for me, because Android requires generating constants fields (http://tools.android.com/tips/non-constant-fields).
I don't know what else to do.
Any tips?

Sorry for the people who asked for an example of code, but it was as basic as that, with a Gradle build.
I fixed the issue by creating a library project within my project. So module A is actually a Library.
I said that I couldn't use library because of the non-constant-field, but that error was generated by AndroidAnnotations, which explains how to create library modules with it:
https://github.com/excilys/androidannotations/wiki/Library-projects#referencing-ids-by-name
Thanks.

Related

The package java.util is accessible from more than one module: error

I am getting an error as "The package java.util is accessible from more than one module: error" while importing file in java.
It seems like you messed things up in your module path. The error message indicates that you depend on multiple java modules that all export the java package java.util.
This is also called split package, as some classes in this package might be loaded from one module the others from another module.
With the new Java module system introduced with Java 9 this is strictly forbidden.
java.util should only be present in java.base module and nowhere else.
If you need further information, please tell us how you built and try to run your application.

Making Maven and module-info work together

I have a project(Java 12) with several Maven dependencies, and now I'm trying to add module-info file like
module mymodule {
requires java.net.http;
}
But if I do this all Maven dependecies (in pom.xml) become invisible for code, and compiler throws errors like java: package org.openqa.selenium.safari is not visible
(package org.openqa.selenium.safari is declared in module selenium.safari.driver, but module mymodule does not read it)
Is it possible to make them work together?
The new module info ist not congruent with the information in the pom.xml. Robert wrote a good article about the differences of both systems:
https://www.sitepoint.com/maven-cannot-generate-module-declaration/

How to define qualified exports to unknown modules?

I have a Maven project with two Maven modules A and B. A contains the following Java module definition:
module A {
exports internal.util to B;
exports external.A;
}
B contains the following Java module definition:
module B {
requires A;
exports external.B;
}
When I build the project, I get an error:
[WARNING] module-info.java:[16,106] module not found: B
Module B exists but because Module A is compiled before B and does not depend on it, the compiler has no way of knowing that. Because I configured the compiler to treat warnings as errors (-Werror), the build fails.
Seeing as I want to keep treating warnings as errors, what is the best way to resolve this problem?
Is there a way to hint to the compiler that this module will be declared in the future?
Is there a way to suppress all warnings of this type?
I figured out a workaround by scanning through the JDK 11 source-code: -Xlint:-module. I am still open to a better solution if someone finds one.
UPDATE: An alternative is to use --module-source-path as demonstrated by https://stackoverflow.com/a/53717183/14731
Thank you Alan Bateman for pointing me in this direction!
As I could infer from the question, your scenario is such that both the module A and B co-exists and the module A does not depend on the module B.
From a module system prospect, since A makes a qualified export to the module B, a warning is thrown if the module B is not resolved on the modulepath while you build A.
On IntelliJ-IDEA I could fix such an issue by adding a dependency of module B to the module A without any declaration changes. Though using the Maven framework, I couldn't imagine something without a cyclic dependency here, maybe the suggestion by khmarbaise could help.
Note: An alternate way to fix this though could also be to disable such warnings using the command-line arg :
-Xlint:-exports
Or as Alan points out for correction
-Xlint:-module
Thinking out loud, that would be since, the warning is a result of a qualified export, but about a module not found.
Useful: You could find the details over these command line args with javac command.

java 9: In module-info.java do i have to again mention packages((as requires) that are already being imported in sources

working with java 9 modules, if i am using java.xml in my code...
1) i will import xml package using import statement...
2) if i don't mention that this package is required in the module declaration of my module...
- will the compilation of my module work.. ??
i would guess... no... and on mentioning that xml package is required on module-info.java... it might work.
so.. what I am wondering is... is that not redundancy... every importing package is implicitly... required. (unless i need to understand module even better)
Is there a way to mention that all imported packages are required in module declaration, other wise it could be a long list to mention in module-info.java?
First of all, in module-info.java you mention modules, not packages. E.g. java.xml is a module which contains about 25 packages. So, if your module uses 10 packages from the java.xml module, you don't have to repeat that 10 times in module-info.java, you write requires java.xml just once. So, that huge list of dependencies is not huge actually.
If you really want to skip all those declarations, you can just not create module-info.java (but I don't recommend to do that). A module that does not have module-info.java is called an automatic module and it implicitly requires all other modules.

Moduled project not compiling using lombok

I'm making a project using lombok. The structur of it is:
Main project:
module A
module B
module C
I created a main function in module A to test the program.
Module A depends on module B and C, module B on anything and module C on module A and B (don't know if this is important to know).
When I run the main function on module A it gives me an error:
Error:java: Annotation processing is not supported for module cycles. Please ensure that all modules from cycle [A,B,C] are excluded from annotation processing
I think it is caused by lombok. Anyone knows how to solve it?

Categories

Resources