• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Why would I want (or not want) to create a "module-info.java" file in Eclipse IDE?

 
Ranch Hand
Posts: 68
Eclipse IDE MySQL Database Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I'm creating a new Java project in Eclipse IDE after I click on "Finish" a new window pops up asking me about creating module-info.java:



What's that about?  DDGing the subject lets me know they started in JDK 9 and let's me somehow limit or include specific code, but what's that mean?  What's an example look like that would make sense to a noob?  I'd guess I won't be needing this for a while, if I select "Don't Create" can I add it later?

Thanks,
~d
 
Saloon Keeper
Posts: 13014
281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, module-info.java was introduced in Java 9, with the Project Jigsaw module system.

A module is a build artifact (usually a Jar file) that contains a module descriptor that declares the name of the module, what other modules it depends on, what packages it exposes to other modules, and what services it implements.

You see, before Java 9 you had to dump all the libraries your application uses on the classpath, and let Java load all of them to figure out that a class you need is available. The module system allows Java to figure out what module a class is in before it loads the module. That way, only modules that you actually need are loaded, and they're loaded at the time they're needed and no sooner.

There's another even bigger advantage though: Before Java 9, the "public" keyword meant that a class can be accessed by every other class in the entire world. If a class was only intended for internal use in your library and you wanted to hide it from the rest of the world, you had to put it in the same package as all the other library classes that use it and make it package private. Since Java 9, you can make classes public and stick them in different packages of your library, but still hide them from classes outside the library, simply by not exporting the package that the class is in.

When you omit module-info.java, you lose these benefits: your library is loaded regardless of whether and when it is needed, and your public classes are exposes to all the world to see.

It's not a disaster if you don't have a module descriptor, but it's recommended that you add one to every project.

If you don't want to do it now, yes, you can always do it later.
 
David Cone
Ranch Hand
Posts: 68
Eclipse IDE MySQL Database Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found an article that explains it more if anyone else is new to the concept.  It's still clear as mud to me, but I'm sure as I'm further along in my java learning,  it will mean more to me.
webpage explaining module-info
 
Stephan van Hulst
Saloon Keeper
Posts: 13014
281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anything in particular that you are having problems with?
 
Today's lesson is that you can't wear a jetpack AND a cape. I should have read this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic