Re: a question about creating the JAR file
I am a bit confused here. My code uses a third party JAR file, helper.jar. Now, I have finished
compiling my code, with helper.jar on the classpath of course. Now it's time to create the
executable JAR(hello.jar) for delivery.
My confusion is do I need to bundle helper.jar into hello.jar?
No, in fact that would be stupid.
One thought I have is that during compiling, all the code needed from helper.jar has been extracted
and combined into the generated *.clss [sic] files in build/ directory.
So, for creating hello.jar, I only need to bundle all *.class files in build/ directory.
But I am not sure that is correct.
It is not.
Currently, I have to unjar(expand) helper.jar into build/ directory before I jar everything in build/.
No, you don't. You didn't read the docs on "jar", did you?
The result hello.jar works fine. If I skip unjarring helper.jar into build/ directory, no complain [sic] for
How are you creating "hello.jar"?
But when running it, an exception of no class definition for a class inside helper.jar is thrown.
Do not paraphrase error messages. Copy and paste them into your post.
How are you running the JAR?
You give such incomplete information!
If you are doing this:
$ java -cp hello.jar your.package.Main
then you need to add the third-party JAR to the "-cp" argument.
I will pretend you are attempting this:
$ java -jar hello.jar
which is the normal way to run a JAR. You should package the JAR with its antecedent JARs
into a delivery vehicle like a ZIP file or some installer package that lets the user unpack the
application ("hello.jar") and its antecedents ("helper.jar") into a controlled directory structure.
Two are common - app and antecedents in the same directory, or antecedents in a "lib/"
subdirectory relative to where the app JAR resides.
So one of these:
||== hello.jar ||== hello.jar
||== lib/ ||== helper.jar
You set up the manifest in the app JAR ("hello.jar") to specify the "Class-Path:" relative to its own
for the "lib/" scenario.
("java -jar" ignores the classpath command argument and the CLASSPATH envar.)