Incremental Modularization, Modular JARs On Java <9, And Auto-Updates

CodeFX Weekly #33 — 2nd of September 2017

Nicolai Parlog
nipafx news
Published in
7 min readSep 6, 2017

--

Hi everyone,

this was an intense week. After our holiday my wife and daughter stayed with my parents in law, which means I had a whole week for myself. I could’ve relaxed or something, but the book’s schedule doesn’t allow that, so instead I worked like an animal, all day every day. I can’t exactly say it was fun, but it was satisfying — I’m thrilled to have finished chapter 7!

I send this newsletter out every Friday. Yes, as an actual email. Subscribe!

Incremental modularization of existing projects

Chapter 7 is packed with everything you need to know for incremental modularization:

  • unnamed and automatic modules and how they help migration and modularization
  • mixing class path and module path
  • incrementally modularizing projects bottom-up, top-down, or inside-out
  • generating module declarations with JDeps
  • hacking third-party JARs with the jar tool
  • publishing modular JARs for Java 8 and older

If you have a project that you plan to migrate to Java 9 and then turn into modules, you’ll need to read it — the larger the project, the more you’ll benefit. It should hit the early-access ebook in about three weeks — you own a copy, right? Right?!

If not, get one now, damn it! I need to eat… (You, too, so use the code fccparlog to get 37% discount.)

I found the last bullet, publishing modular JARs for Java versions before 9 particularly interesting, so I want to share it with you.

Publishing modular JARs for Java 8 and older

Whether you maintain an application, a library, or a framework, it is entirely possible that you target more than one Java version. Does that mean you have to skip the module system? Fortunately, not! There are two ways to deliver modular artifacts that work just fine on Java versions older than 9.

Whichever you chose, you first need to build your project for the target version. You can either use the compiler from the corresponding JDK or a newer one by setting -source and -target. If you pick the Java 9 compiler, check out the new --release flag. Finish this step by creating a JAR as you normally would. Note that this JAR runs perfectly fine on your desired Java release.

Next step is to compile the module declarations with Java 9. The best and most reliable way is to simply build the entire project with the Java 9 compiler.

Now you have two options for how to get the module descriptor into your JAR.

Just use jar --update

You can simply use jar --update to add the module descriptor to the JAR:

jar --update --file ${jar_file} module-info.class

This adds module-info.class to ${jar_file}. Note that --update does not perform any checks. This makes it easy to, accidentally or on purpose, create JARs whose module descriptor and class files do not agree, for example on required dependencies. Use with care!

Adding the module descriptor is not a problem for older JVMs because they actually ignore it. They only see other class files and because you build them for the correct version, everything just works.

While that is true for the JVM, it can not necessarily be said for other tools. Some trip over module-info.class and thus become useless for modular JARs. If you want to prevent that you have to create a multi-release JAR.

Creating multi-release JARs

Multi-release JARs are an entire topic of their own — the gist is that they contain bytecode for several major Java versions, where…

  • … Java 8 and older load the version-unspecific bytecode
  • … Java 9 and newer load the version-unspecific bytecode and overlay it with the version-specific code

Multi-release JARs store version-specific class files in META-INF/versions/${version}, where they should be safe from bytecode scanners.

To create a multi-release JAR use the new jar flag --release ${release}, followed by the common way to list class files (directly or with -C):

jar --update
--file ${jar_file}
--release 9 module-info.class

Conference Season

The fall 2017 conference season is upon us and I’ve got my speaking calendar all set up — here’s where I’ll be giving presentations:

JavaForum Nord in Hannover, Germany

  • September 12th
  • “Java 9 Is Here!”

JavaZone in Oslo, Norway

  • September 13th
  • “To JAR Hell And Back — A Live Migration to the Java 9 Module System”

Objektforum Stuttgart in Stuttgart, Germany

  • September 18th
  • “Expert Java 8”

Objektforum Frankfurt in Frankfurt, Germany

  • September 26th
  • “Java 9 Is Here!”

BaselOne in Base, Switzerland

  • October 19th
  • “The Java 9 Module System Beyond the Basics”

JavaDay in Kiev, Ukraine

  • November 4th or 5th
  • “To JAR Hell And Back — A Live Migration to the Java 9 Module System”

Gesellschaft für Informatik in Dortmund, Germany

  • December, 4th
  • “Neues in Java 9”

Code Europe in Wroclaw, Poland

  • December 13th
  • “The Java 9 Module System Beyond the Basics”

Let me know if you’re there as well and want to meet up.

Just ask

Related is this tweet I send in reply to a longish flowchart which essentially said “Think very carefully before asking a speaker a question”:

Can’t speak for others, but if it’s me up there, screw that flow chart and just ask. I’m happy to answer all your questions!

So please, just ask! It’s not like the average talk suffers from too many questions.

This also holds for the rest of the conference when I’m not on stage. If you see me and think I can help you with something or you just want to chat, just flag me down. I’d be happy to meet you!

Auto-update to Java 9

Over at StackOverflow a developer wondered whether the Java auto-updater would install Java 9 right on September 21st. Short Answer: No. Long answer:

New Java versions take some time to percolate to unsuspecting users:

  • Even after the GA release of Java 9 will Java 8 remain the default download option at Java SE Downloads for about six months. (I’m quite sure about this, but don’t have a source for it.)
  • Java 8 will receive further public updates until at least September 2018.
  • Only when that time window draws to a close and Oracle published its official “end of public updates notice” will hesitant users have to consider switching to Java 9.
  • A few months before the end of public support will the auto-updater propose an update to Java 9, but it will require user permission. (At least that’s how it worked between Java 7 and 8).

That other thing

During the last two weeks I put a new thing into the pipeline. Not that I have time for it, but it wanted to get out of me for a few months now, and badly! While I relaxed on the beach, it gathered strength and one sunny day tore down the wall of reason and jumped the moat of caution.

I really wanted to present you a sneak preview this week, but, alas, I didn’t get around to it. Maybe next week or the week after, but the absolute latest you will hear about this is September 21st. I’d tell you to mark that date in your calendar, but it should be marked already with “Java 9 Release!”.

Coincidence?

Breaking Bad

Started to watch Breaking Bad again — so fucking awesome! Thrilling to observe all the early signs of Walt’s evolution from meek teacher to brutal kingpin. How he pushes Jessie over the edge again and again. How his face hardens and his eyes get icier — a little more each time ruthlessness wins over conscience. He surely is the one who knocks.

(Breaking Bad DVD/Blu-ray box on Amazon com, uk, de, ca, fr.)

PS: Don’t forget to subscribe or recommend! :)

--

--

Nicolai Parlog
nipafx news

Nicolai is a #Java enthusiast with a passion for learning and sharing — in posts & books; in videos & streams; at conferences & in courses. https://nipafx.dev