You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@maven.apache.org by DCVer <ke...@o2.pl> on 2007/06/26 14:59:51 UTC

adding the classpath to jar file

Hi,

I'm creating a jar using "mvn package". The files, which are included in
this jar are using another jar (e.g. log4j-1.2.14.jar), which are stored in
maven repository. My part of pom.xml file responsible for dependencies looks
like:

<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.14</version>
  <scope>compile</scope>
  <type>jar</type>
</dependency>

The "mvn package" process finishes correctly, but when i run the jar using:

java -cp target\my-app-1.0-SNAPSHOT.jar com.mycompany.app.App

i receive the error:

Exception in thread "main" java.lang.NoClassDefFoundError:
org/apache/log4j/Logger

(on line:
private static Logger logger = Logger.getLogger("com.mycompany.app.App");

In my opinion the jar, that was created using "mvn package" doesn't know
about dependencies (and about jars stored in repository?). How should I set
classpaths and make this jar work correctly? Isn't it a BASIC functionality,
that Maven2 should do, when I set <dependency> in pom.xml file?

I tried this with other jars (in repository) and other methods of the
classes included in jars and the results were exactly the same.
-- 
View this message in context: http://www.nabble.com/adding-the-classpath-to-jar-file-tf3982263s177.html#a11305225
Sent from the Maven - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: adding the classpath to jar file

Posted by Keith Beattie <ks...@gmail.com>.
On 6/26/07, DCVer <ke...@o2.pl> wrote:
>
>
> Ok, thanks for some information!


I've gotten Maven to create an executable jar which sets a class-path in its
manifest file  pointing (via a *relative* path) to contained dependent jars
within the same jar though the use of the jar and assembly plugins [1].  So
this *is* possible but of little use beyond just having them all in a single
container due to the classloading problem [2].

[1] http://dsd.lbl.gov/~ksb/Scratch/Maven_notes.html<http://dsd.lbl.gov/%7Eksb/Scratch/Maven_notes.html>
[2] http://one-jar.sourceforge.net/

ksb

Re: adding the classpath to jar file

Posted by Wayne Fay <wa...@gmail.com>.
Maven is a BUILD tool not a RUNTIME tool. So your assumptions about
what Maven will do for you regarding your dependencies etc at runtime
are simply invalid. Dependency jars do not necessarily live in the
same location on all your machines.

If you require a single jar that "just runs", you can build an "uber
jar" containing all your dependencies etc along with your own project
jars using the Assembly plugin and "jar-with-dependencies"
configuration. This has been discussed numerous times on this list and
is well-documented on the Maven website.

Wayne

On 6/26/07, DCVer <ke...@o2.pl> wrote:
>
> Ok, thanks for some information!
>
>
> Jan-Christopher Bals-2 wrote:
> >
> > Yes. The behavior is correct.
> >
> > Maven does not include the log4j jar because jars cannot contain other
> > jars.
> >
> > When you use additional jars at runtime, you have to provide them via the
> > classpath.
> >
> > And as far as i know you cannot define the classpath of jar inside the jar
> > like the main class.
> >
> > So maven cannot set the classpath for you. Moreover Maven cannot know
> > where
> > the jar is executed, so it cannot guarantee that the referenced jars in
> > the
> > classpath are present.
> >
> > 2007/6/26, DCVer <ke...@o2.pl>:
> >>
> >>
> >> Thanks for reply.
> >>
> >> Hmm, I think my jar doesn't contain log4j - it only contains App.class
> >> and
> >> pom.xml with pom.properties and manifest.mf. Is it a correctly built jar?
> >>
> >> And I wonder why Maven2 won't set *somewhere* classpaths to jars on its
> >> own
> >> if it knows where they are :-/
> >>
> >>
> >> Jan-Christopher Bals-2 wrote:
> >> >
> >> > You have to provide log4j via the classpath on your own.
> >> > Maven does include log4j in your generated jar automatically.
> >> >
> >> > But Maven can generate the classpath for you
> >> > http://maven.apache.org/plugins/maven-dependency-plugin/usage.html
> >> > ->dependency:build-classpath
> >> >
> >> >
> >> > 2007/6/26, DCVer <ke...@o2.pl>:
> >> >>
> >> >>
> >> >> Hi,
> >> >>
> >> >> I'm creating a jar using "mvn package". The files, which are included
> >> in
> >> >> this jar are using another jar (e.g. log4j-1.2.14.jar), which are
> >> stored
> >> >> in
> >> >> maven repository. My part of pom.xml file responsible for dependencies
> >> >> looks
> >> >> like:
> >> >>
> >> >> <dependency>
> >> >>   <groupId>log4j</groupId>
> >> >>   <artifactId>log4j</artifactId>
> >> >>   <version>1.2.14</version>
> >> >>   <scope>compile</scope>
> >> >>   <type>jar</type>
> >> >> </dependency>
> >> >>
> >> >> The "mvn package" process finishes correctly, but when i run the jar
> >> >> using:
> >> >>
> >> >> java -cp target\my-app-1.0-SNAPSHOT.jar com.mycompany.app.App
> >> >>
> >> >> i receive the error:
> >> >>
> >> >> Exception in thread "main" java.lang.NoClassDefFoundError:
> >> >> org/apache/log4j/Logger
> >> >>
> >> >> (on line:
> >> >> private static Logger logger = Logger.getLogger("com.mycompany.app.App
> >> ");
> >> >>
> >> >> In my opinion the jar, that was created using "mvn package" doesn't
> >> know
> >> >> about dependencies (and about jars stored in repository?). How should
> >> I
> >> >> set
> >> >> classpaths and make this jar work correctly? Isn't it a BASIC
> >> >> functionality,
> >> >> that Maven2 should do, when I set <dependency> in pom.xml file?
> >> >>
> >> >> I tried this with other jars (in repository) and other methods of the
> >> >> classes included in jars and the results were exactly the same.
> >> >> --
> >> >> View this message in context:
> >> >>
> >> http://www.nabble.com/adding-the-classpath-to-jar-file-tf3982263s177.html#a11305225
> >> >> Sent from the Maven - Users mailing list archive at Nabble.com.
> >> >>
> >> >>
> >> >> ---------------------------------------------------------------------
> >> >> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> >> >> For additional commands, e-mail: users-help@maven.apache.org
> >> >>
> >> >>
> >> >
> >> >
> >>
> >> --
> >> View this message in context:
> >> http://www.nabble.com/adding-the-classpath-to-jar-file-tf3982263s177.html#a11305662
> >> Sent from the Maven - Users mailing list archive at Nabble.com.
> >>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> >> For additional commands, e-mail: users-help@maven.apache.org
> >>
> >>
> >
> >
>
> --
> View this message in context: http://www.nabble.com/adding-the-classpath-to-jar-file-tf3982263s177.html#a11306165
> Sent from the Maven - Users mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: adding the classpath to jar file

Posted by DCVer <ke...@o2.pl>.
Ok, thanks for some information!


Jan-Christopher Bals-2 wrote:
> 
> Yes. The behavior is correct.
> 
> Maven does not include the log4j jar because jars cannot contain other
> jars.
> 
> When you use additional jars at runtime, you have to provide them via the
> classpath.
> 
> And as far as i know you cannot define the classpath of jar inside the jar
> like the main class.
> 
> So maven cannot set the classpath for you. Moreover Maven cannot know
> where
> the jar is executed, so it cannot guarantee that the referenced jars in
> the
> classpath are present.
> 
> 2007/6/26, DCVer <ke...@o2.pl>:
>>
>>
>> Thanks for reply.
>>
>> Hmm, I think my jar doesn't contain log4j - it only contains App.class
>> and
>> pom.xml with pom.properties and manifest.mf. Is it a correctly built jar?
>>
>> And I wonder why Maven2 won't set *somewhere* classpaths to jars on its
>> own
>> if it knows where they are :-/
>>
>>
>> Jan-Christopher Bals-2 wrote:
>> >
>> > You have to provide log4j via the classpath on your own.
>> > Maven does include log4j in your generated jar automatically.
>> >
>> > But Maven can generate the classpath for you
>> > http://maven.apache.org/plugins/maven-dependency-plugin/usage.html
>> > ->dependency:build-classpath
>> >
>> >
>> > 2007/6/26, DCVer <ke...@o2.pl>:
>> >>
>> >>
>> >> Hi,
>> >>
>> >> I'm creating a jar using "mvn package". The files, which are included
>> in
>> >> this jar are using another jar (e.g. log4j-1.2.14.jar), which are
>> stored
>> >> in
>> >> maven repository. My part of pom.xml file responsible for dependencies
>> >> looks
>> >> like:
>> >>
>> >> <dependency>
>> >>   <groupId>log4j</groupId>
>> >>   <artifactId>log4j</artifactId>
>> >>   <version>1.2.14</version>
>> >>   <scope>compile</scope>
>> >>   <type>jar</type>
>> >> </dependency>
>> >>
>> >> The "mvn package" process finishes correctly, but when i run the jar
>> >> using:
>> >>
>> >> java -cp target\my-app-1.0-SNAPSHOT.jar com.mycompany.app.App
>> >>
>> >> i receive the error:
>> >>
>> >> Exception in thread "main" java.lang.NoClassDefFoundError:
>> >> org/apache/log4j/Logger
>> >>
>> >> (on line:
>> >> private static Logger logger = Logger.getLogger("com.mycompany.app.App
>> ");
>> >>
>> >> In my opinion the jar, that was created using "mvn package" doesn't
>> know
>> >> about dependencies (and about jars stored in repository?). How should
>> I
>> >> set
>> >> classpaths and make this jar work correctly? Isn't it a BASIC
>> >> functionality,
>> >> that Maven2 should do, when I set <dependency> in pom.xml file?
>> >>
>> >> I tried this with other jars (in repository) and other methods of the
>> >> classes included in jars and the results were exactly the same.
>> >> --
>> >> View this message in context:
>> >>
>> http://www.nabble.com/adding-the-classpath-to-jar-file-tf3982263s177.html#a11305225
>> >> Sent from the Maven - Users mailing list archive at Nabble.com.
>> >>
>> >>
>> >> ---------------------------------------------------------------------
>> >> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
>> >> For additional commands, e-mail: users-help@maven.apache.org
>> >>
>> >>
>> >
>> >
>>
>> --
>> View this message in context:
>> http://www.nabble.com/adding-the-classpath-to-jar-file-tf3982263s177.html#a11305662
>> Sent from the Maven - Users mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
>> For additional commands, e-mail: users-help@maven.apache.org
>>
>>
> 
> 

-- 
View this message in context: http://www.nabble.com/adding-the-classpath-to-jar-file-tf3982263s177.html#a11306165
Sent from the Maven - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: adding the classpath to jar file

Posted by Jan-Christopher Bals <ja...@googlemail.com>.
Yes. The behavior is correct.

Maven does not include the log4j jar because jars cannot contain other jars.

When you use additional jars at runtime, you have to provide them via the
classpath.

And as far as i know you cannot define the classpath of jar inside the jar
like the main class.

So maven cannot set the classpath for you. Moreover Maven cannot know where
the jar is executed, so it cannot guarantee that the referenced jars in the
classpath are present.

2007/6/26, DCVer <ke...@o2.pl>:
>
>
> Thanks for reply.
>
> Hmm, I think my jar doesn't contain log4j - it only contains App.class and
> pom.xml with pom.properties and manifest.mf. Is it a correctly built jar?
>
> And I wonder why Maven2 won't set *somewhere* classpaths to jars on its
> own
> if it knows where they are :-/
>
>
> Jan-Christopher Bals-2 wrote:
> >
> > You have to provide log4j via the classpath on your own.
> > Maven does include log4j in your generated jar automatically.
> >
> > But Maven can generate the classpath for you
> > http://maven.apache.org/plugins/maven-dependency-plugin/usage.html
> > ->dependency:build-classpath
> >
> >
> > 2007/6/26, DCVer <ke...@o2.pl>:
> >>
> >>
> >> Hi,
> >>
> >> I'm creating a jar using "mvn package". The files, which are included
> in
> >> this jar are using another jar (e.g. log4j-1.2.14.jar), which are
> stored
> >> in
> >> maven repository. My part of pom.xml file responsible for dependencies
> >> looks
> >> like:
> >>
> >> <dependency>
> >>   <groupId>log4j</groupId>
> >>   <artifactId>log4j</artifactId>
> >>   <version>1.2.14</version>
> >>   <scope>compile</scope>
> >>   <type>jar</type>
> >> </dependency>
> >>
> >> The "mvn package" process finishes correctly, but when i run the jar
> >> using:
> >>
> >> java -cp target\my-app-1.0-SNAPSHOT.jar com.mycompany.app.App
> >>
> >> i receive the error:
> >>
> >> Exception in thread "main" java.lang.NoClassDefFoundError:
> >> org/apache/log4j/Logger
> >>
> >> (on line:
> >> private static Logger logger = Logger.getLogger("com.mycompany.app.App
> ");
> >>
> >> In my opinion the jar, that was created using "mvn package" doesn't
> know
> >> about dependencies (and about jars stored in repository?). How should I
> >> set
> >> classpaths and make this jar work correctly? Isn't it a BASIC
> >> functionality,
> >> that Maven2 should do, when I set <dependency> in pom.xml file?
> >>
> >> I tried this with other jars (in repository) and other methods of the
> >> classes included in jars and the results were exactly the same.
> >> --
> >> View this message in context:
> >>
> http://www.nabble.com/adding-the-classpath-to-jar-file-tf3982263s177.html#a11305225
> >> Sent from the Maven - Users mailing list archive at Nabble.com.
> >>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> >> For additional commands, e-mail: users-help@maven.apache.org
> >>
> >>
> >
> >
>
> --
> View this message in context:
> http://www.nabble.com/adding-the-classpath-to-jar-file-tf3982263s177.html#a11305662
> Sent from the Maven - Users mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>
>

Re: adding the classpath to jar file

Posted by DCVer <ke...@o2.pl>.
Thanks for reply.

Hmm, I think my jar doesn't contain log4j - it only contains App.class and
pom.xml with pom.properties and manifest.mf. Is it a correctly built jar?

And I wonder why Maven2 won't set *somewhere* classpaths to jars on its own
if it knows where they are :-/


Jan-Christopher Bals-2 wrote:
> 
> You have to provide log4j via the classpath on your own.
> Maven does include log4j in your generated jar automatically.
> 
> But Maven can generate the classpath for you
> http://maven.apache.org/plugins/maven-dependency-plugin/usage.html
> ->dependency:build-classpath
> 
> 
> 2007/6/26, DCVer <ke...@o2.pl>:
>>
>>
>> Hi,
>>
>> I'm creating a jar using "mvn package". The files, which are included in
>> this jar are using another jar (e.g. log4j-1.2.14.jar), which are stored
>> in
>> maven repository. My part of pom.xml file responsible for dependencies
>> looks
>> like:
>>
>> <dependency>
>>   <groupId>log4j</groupId>
>>   <artifactId>log4j</artifactId>
>>   <version>1.2.14</version>
>>   <scope>compile</scope>
>>   <type>jar</type>
>> </dependency>
>>
>> The "mvn package" process finishes correctly, but when i run the jar
>> using:
>>
>> java -cp target\my-app-1.0-SNAPSHOT.jar com.mycompany.app.App
>>
>> i receive the error:
>>
>> Exception in thread "main" java.lang.NoClassDefFoundError:
>> org/apache/log4j/Logger
>>
>> (on line:
>> private static Logger logger = Logger.getLogger("com.mycompany.app.App");
>>
>> In my opinion the jar, that was created using "mvn package" doesn't know
>> about dependencies (and about jars stored in repository?). How should I
>> set
>> classpaths and make this jar work correctly? Isn't it a BASIC
>> functionality,
>> that Maven2 should do, when I set <dependency> in pom.xml file?
>>
>> I tried this with other jars (in repository) and other methods of the
>> classes included in jars and the results were exactly the same.
>> --
>> View this message in context:
>> http://www.nabble.com/adding-the-classpath-to-jar-file-tf3982263s177.html#a11305225
>> Sent from the Maven - Users mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
>> For additional commands, e-mail: users-help@maven.apache.org
>>
>>
> 
> 

-- 
View this message in context: http://www.nabble.com/adding-the-classpath-to-jar-file-tf3982263s177.html#a11305662
Sent from the Maven - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: adding the classpath to jar file

Posted by Jan-Christopher Bals <ja...@googlemail.com>.
You have to provide log4j via the classpath on your own.
Maven does include log4j in your generated jar automatically.

But Maven can generate the classpath for you
http://maven.apache.org/plugins/maven-dependency-plugin/usage.html
->dependency:build-classpath


2007/6/26, DCVer <ke...@o2.pl>:
>
>
> Hi,
>
> I'm creating a jar using "mvn package". The files, which are included in
> this jar are using another jar (e.g. log4j-1.2.14.jar), which are stored
> in
> maven repository. My part of pom.xml file responsible for dependencies
> looks
> like:
>
> <dependency>
>   <groupId>log4j</groupId>
>   <artifactId>log4j</artifactId>
>   <version>1.2.14</version>
>   <scope>compile</scope>
>   <type>jar</type>
> </dependency>
>
> The "mvn package" process finishes correctly, but when i run the jar
> using:
>
> java -cp target\my-app-1.0-SNAPSHOT.jar com.mycompany.app.App
>
> i receive the error:
>
> Exception in thread "main" java.lang.NoClassDefFoundError:
> org/apache/log4j/Logger
>
> (on line:
> private static Logger logger = Logger.getLogger("com.mycompany.app.App");
>
> In my opinion the jar, that was created using "mvn package" doesn't know
> about dependencies (and about jars stored in repository?). How should I
> set
> classpaths and make this jar work correctly? Isn't it a BASIC
> functionality,
> that Maven2 should do, when I set <dependency> in pom.xml file?
>
> I tried this with other jars (in repository) and other methods of the
> classes included in jars and the results were exactly the same.
> --
> View this message in context:
> http://www.nabble.com/adding-the-classpath-to-jar-file-tf3982263s177.html#a11305225
> Sent from the Maven - Users mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>
>