Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #19519

Re: a question about creating the JAR file

Newsgroups comp.lang.java.programmer
Date 2012-10-26 14:14 -0700
References <c34600e7-78d9-44a5-a79c-ee804ed992e2@googlegroups.com>
Message-ID <886c48ec-4c59-4a04-bab3-7e30dccb2edf@googlegroups.com> (permalink)
Subject Re: a question about creating the JAR file
From Lew <lewbloch@gmail.com>

Show all headers | View raw


zyng wrote:
> 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
> creating hello.jar. 

How are you creating "hello.jar"?

Be specific.

> 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:

  somedirectory/               somedirectory/
   ||                                      ||
   ||== hello.jar                   ||== hello.jar
   ||== lib/                          ||== helper.jar
            ||== helper.jar

You set up the manifest in the app JAR ("hello.jar") to specify the "Class-Path:" relative to its own 
location, so 

  Class-Path:  lib/helper.jar

for the "lib/" scenario. 
("java -jar" ignores the classpath command argument and the CLASSPATH envar.)

RTFM.
http://docs.oracle.com/javase/7/docs/technotes/guides/jar/index.html

-- 
Lew

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

a question about creating the JAR file zyng <xsli2@yahoo.com> - 2012-10-26 13:58 -0700
  Re: a question about creating the JAR file Lew <lewbloch@gmail.com> - 2012-10-26 14:14 -0700
    Re: a question about creating the JAR file zyng <xsli2@yahoo.com> - 2012-10-31 12:41 -0700
      Re: a question about creating the JAR file Lew <lewbloch@gmail.com> - 2012-10-31 15:19 -0700
  Re: a question about creating the JAR file Sven Köhler <remove-sven.koehler@gmail.com> - 2012-10-27 16:54 +0200
    Re: a question about creating the JAR file Jan Burse <janburse@fastmail.fm> - 2012-10-27 17:20 +0200
      Re: a question about creating the JAR file Jan Burse <janburse@fastmail.fm> - 2012-10-27 17:24 +0200
        Re: a question about creating the JAR file Sven Köhler <remove-sven.koehler@gmail.com> - 2012-10-27 19:06 +0200
    Re: a question about creating the JAR file Lew <lewbloch@gmail.com> - 2012-10-28 09:44 -0700
  Re: a question about creating the JAR file Roedy Green <see_website@mindprod.com.invalid> - 2012-11-01 18:26 -0700

csiph-web