JEP 476: Clarification on Package Export Behavior in Java Modules

Alex Buckley alex.buckley at oracle.com
Fri Sep 13 15:41:46 UTC 2024


I assume that the package my.nice.project is colocated with the other 
two packages -- my.nice.project.pkg.{a,b} -- in the module my.nice.project.

If so, then all three packages should be able to access each others' 
public types (SomeInterface, SomeRecord) without error.

The fact that you're getting an error looks like a javac bug.

You don't need to `import module my.nice.project` in the source files of 
that module. Try removing those imports and see if my.nice.project.Main 
compiles (it should).

Alex

On 9/13/2024 5:00 AM, Thiago Henrique Hupner wrote:
> Dear Amber Team,
> 
> I hope you're doing well. I’ve encountered a behavior related to JEP 
> 476: Module Import Declarations and wanted to ask for clarification.
> 
> I’m working with a module setup where two packages are defined but not 
> initially exported:
> 
> |module my.nice.project { //exports my.nice.project.pkg.a to 
> my.nice.project; //exports my.nice.project.pkg.b to my.nice.project; } |
> 
> Here’s a simplified version of the code structure:
> 
> 
> |package my.nice.project;
> |
> |
> |import module my.nice.project;|
> 
> |
> |public class Main { public static void main(String[] args) 
> { SomeInterface someInterface = new SomeInterface() { @Override public 
> SomeRecord someMethod() { return new SomeRecord(); } }; } }|
> 
> |
> |
> |package my.nice.project.pkg.a; import module my.nice.project;|
> |
> |
> |public interface SomeInterface { public SomeRecord someMethod(); }|
> 
> |package my.nice.project.pkg.b; public record SomeRecord() {} |
> 
> When attempting to compile this code with the exports commented out, I 
> received several "cannot find symbol" errors. However, after 
> uncommenting the exports:
> 
> 
> |exports my.nice.project.pkg.a to my.nice.project; exports 
> my.nice.project.pkg.b to my.nice.project; |
> 
> The code compiled and ran as expected.
> 
> Given this behavior, I wanted to confirm if this restriction on package 
> visibility within a module is the intended design under the Amber 
> project and the module system, or if there’s another approach or best 
> practice I might be overlooking in terms of package exports.
> 
> Thank you for your time and insight.
> 
> Best regards,
> 
> Thiago
> 



More information about the amber-dev mailing list