javajava-9java-modulejavapackager

unable to make module-based jar with javapackager


I am new to javapackager and Java 9, and am working through the examples in the javapackager reference, with the first example shown below:

Example 1 - Using the -createjar Command

javapackager -createjar -appclass package.ClassName
  -srcdir classes -outdir out -outfile outjar -v

Packages the contents of the classes directory to outjar.jar, 
sets the application class to package.ClassName.

I am able to make the jar file (tcdmod.jar) without an error messages. But I get an error when I try to execute the jar with the normal command:

java -jar tcdmod.jar

Error: Could not find or load main class moduleTCD.com.adonax.tanpura.TCDLaunch Caused by: java.lang.NoClassDefFoundError: com/adonax/tanpura/TCDLaunch (wrong name: moduleTCD/com/adonax/tanpura/TCDLaunch)

Command used for making the jar:

javapackager -createjar -appclass moduleTCD/com.adonax.tanpura.TCDLaunch
-srcdir compiled -outdir outex1 -outfile tcdmod -v

Folder for compiled:

compiled/moduleTCD/com/adonax/tanpura/ [compiled classes here and below]
compiled/moduleTCD/module-info.class

The manifest in the jar shows:

Main-Class: moduleTCD/com.adonax.tanpura.TCDLaunch

Things I've tried so far to solve this:

Any thoughts? In order to make a self-contained exe package (the main goal), I need to have a module-based jar file, if I am reading the documentation correctly.


Solution

  • I finally figured this out by taking a closer look at the syntax for the jar command. (Something I never did in the past thanks to Eclipse IDE handling this automatically.)

    Here is what I discovered and fixed.

    1) The -appclass argument should be the package location of the main, and does not include the module in which the package resides.

    Thus, I changed the argument from "moduleTCD/com.adonax.tanpura.TCDLaunch" to "com.adonax.tanpura.TCDLaunch".

    2) The -srcdir argument should be the module folder (containing module-info.class). Thus, I changed the argument from "compiled" to "compiled/src/moduleTCD".

    So, for my particular situation (only one module, no additional jars or libraries to link up), the following command (as a single line) worked correctly.

    javapackager -createjar -appclass com.adonax.tanpura.TCDLaunch -srcdir
    compiled/src/moduleTCD -outdir outex1 -outfile tcdmod -v
    

    Here is the (single line) jar command that also works.

    jar -cfe outex1.tcdmod.jar com.adonax.tanpura.TCDLaunch -C 
    compiled/src/moduleTCD .