2

I am Working on a javafx project where i am in need to generate module-info.class file myself. So, Iam wondering how to do it properly and make it work with maven.

Iam facing this issue with a QRCode generator library zxing. I cannot find any alternative Qr generator libraries in java with module-info.class file , if any please mention it.

I have to do this, since i use jlink which throws "automatic module cannot be used with jlink" error.

breif explanation would be very helpful :)

note: I have tried to inject the module file using a tool but it throws error: cannot read jar

8
  • 1
    Without Maven, the basic steps are: (1) Generate the module-info descriptor with jdeps (see this); (2) Compile the module-info source file with javac (see this); (3) Add the module-info class file to the JAR file with jar. Commented Dec 10, 2023 at 15:39
  • There may be Maven plugins out there that already do this for you. For example, moditect seems to do this, though I've never used it. Commented Dec 10, 2023 at 15:44
  • If you're using Java 9 or higher, just add a module-info.java file to the root of your source folder (i.e. with no package), and include it in compilation. When using Maven, just put it directly in src/main/java. If you're using Java 8 then Moditect works great. I'm using it in my Java 8 libraries without any problems. Commented Dec 10, 2023 at 18:35
  • 2
    @Slaw Thank you i have successfully injected the file into the library and jlink is working perfectly. I'm getting some errors after running my application from the generated zip file from jlink i'll figure it out Commented Dec 11, 2023 at 5:35
  • 1
    @Rob Spoor jlink throws an error that says "automatic module cannot be used with jlink" that's why i had to inject the module-info file and everything works fine now. I haven't tried adding those transient dependencies to my own module-info file i might try it later thank you. Commented Dec 13, 2023 at 17:27

1 Answer 1

5

I have fed up with module injector tools so ,I tried to do it with jdeps

Follow these simple step to inject the module-info properly

  1. Copy the jar file (in my case zxing package i.e core-3.4.1.jar) into a folder (here i copied it into libs folder)

  2. open cmd and navigate to parent folder of libs folder .

now run the command,

jdeps --ignore-missing-deps --generate-module-info libs libs/core-3.4.1.jar 

It creates a module-info.java inside libs folder or inside one with its module name(in my case com.google.zxing/module-info.java).

  1. copy the module-info.java file and paste it inside the libs folder

  2. Run the command,

javac --patch-module com.google.zxing=jars/core-3.4.1.jar jars/module-info.java

it will generate the module-info.class file in the libs folder.

  1. Now to inject the file into the jar,run
jar uf jars/core-3.4.1.jar -C jars module-info.class

Now your module-info.class file will be injected inside the jar which you can use it in you modular javafx project

note: Don't do this unless there is no option. In my case iam using the zxing library to generate QR code and i couldn't find a good alternative for it.

Sign up to request clarification or add additional context in comments.

3 Comments

There should not be a need no need to do any patching, zxing core is an automatic module. All packages are exported, only the transitive dependencies are missing and need to be added to the project's own module-info.java if those are needed.
@RobSpoor Automatic modules can make it extremely difficult to build native packages though. This may be an easier option if the OP wants to build a modular application with jlink and/or jpackage.
@RobSpoor Automatic modules cannot be used with jlink, even if the JAR's manifest has an Automatic-Module-Name entry. It's not clear in the question body that jlink is being used, but the question is tagged with jlink. That said, non-modular code can be packaged with jpackage with the --input option, but that may not be desirable, or even an option, for the OP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.