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:
I could not tell from documentation if I specified the -appclass parameter correctly. Documentation defines -appclass as follows: "Qualified name of the application class to be executed." But I don't see any indication of what is meant by "qualified". I tried a few different names that seemed plausible. Is the form with the module name that I used correct?
I created a second "compiled" directory in which the module was simply omitted from the project, and retried the command using that for my -srcdir. The resulting jar file worked fine in that case.
I verified that I could run the program from the command line using the compiled source (from the "compiled" folder):
java -p . -m moduleTCD/com.adonax.tanpura.TCDLaunch
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.
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 .