Search code examples
javajavafxzxingjlinkmodule-info

How to create a module-info class file and add it to the jar?


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


Solution

  • 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.