You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Apache Wiki <wi...@apache.org> on 2014/07/29 07:59:54 UTC

[Commons Wiki] Update of "MavenAndClasspath" by sebbapache

Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Commons Wiki" for change notification.

The "MavenAndClasspath" page has been changed by sebbapache:
https://wiki.apache.org/commons/MavenAndClasspath

New page:
This page describes why Maven coordinates and Java class package names need to be co-ordinated.

== Maven classpath ==

Maven identifies jars using the coordinate triple {groupId, artifactId, version}.
It assumes that jars with the same pair {groupId, artifactId} are different versions of the same jar,
and will ensure that only one instance of the jar is added to the classpath (in general the one with the highest version).

Jars with different {groupId, artifactId} pairs are treated as entirely different jars, even if they contain identical classes.
Maven does not examine the contents of jars; it relies on the {groupId, artifactId} pair to determine whether different jars have the same or different classes in them.

== Java classloader ==

The Java runtime system only allows a single instance of a class to exist in a given classloader.
Classes are uniquely identified using the package name and class name.

For example, the class {{{Utils}}} in the package {{{org.apache.commons.example}}} can only appear once in a classloader.

The classloader uses the classpath to find the classes needed by an application.

If the classpath contains more than one jar which contains the above Utils class, then the JVM will load only one of the instances.

If the class instances in the different jars are identical, then this is not a problem, but if there are two different versions of the class, the classloader may choose the wrong one. And different JVMs may process the classpath in a different order.

To ensure that the expected class is loaded, it is vital that the classpath only contain a single instance of each different class.
This is normally achieved by grouping classes in jars according to their package names, and ensuring that there is only one instance of each such jar on the classpath.

==Maven coords and package names==

When using Maven to construct the classpath, it is therefore essential that Maven knows which jars hold different versions of the same classes and which jars have completely different classes.

Therefore, it is vital that two jars with the same {groupId, artifactId} pair use the same package name.
Likewise, it is vital that two jars with the same package name use the same {groupId, artifactId} pair.

----

'''There must be a 1-1 correspondance between the Maven {groupId, artifactId} pair and the Java package name.'''

If either/both of the Maven groupId or artifactId is changed, the package name must be changed.
Otherwise, Maven may add more than one copy of a class to the classpath (in different jars) causing unpredictable behaviour.

Likewise, a change of package name (e.g. for a binary break) must be accompanied by a change of either Maven groupId or artifactId.
Otherwise, Maven won't be able to to add the original package name to the classpath, breaking code that relies on the original package name.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [Commons Wiki] Update of "MavenAndClasspath" by sebbapache

Posted by Gilles <gi...@harfang.homelinux.org>.
On Tue, 29 Jul 2014 12:12:46 +0200, Jörg Schaible wrote:
> Gilles wrote:
>
>>> [...]
>>>
>>> == Java classloader ==
>>>
>>> The Java runtime system only allows a single instance of a class to
>>> exist in a given classloader.
>>> Classes are uniquely identified using the package name and class
>>> name.
>>>
>>> For example, the class {{{Utils}}} in the package
>>> {{{org.apache.commons.example}}} can only appear once in a
>>> classloader.
>>>
>>> The classloader uses the classpath to find the classes needed by an
>>> application.
>>>
>>> If the classpath contains more than one jar which contains the 
>>> above
>>> Utils class, then the JVM will load only one of the instances.
>>>
>>> If the class instances in the different jars are identical, then 
>>> this
>>> is not a problem, but if there are two different versions of the
>>> class, the classloader may choose the wrong one. And different JVMs
>>> may process the classpath in a different order.
>>
>> Is that correct?
>>
>> Excerpt from
>> 
>> http://docs.oracle.com/javase/8/docs/technotes/tools/unix/classpath.html:
>> -----
>> The order in which you specify multiple class path entries is
>> important.
>> The Java interpreter will look for classes in the directories in the
>> order they appear in the class path variable.
>> -----
>>
>> Gilles
>>
>>> [...]
>
> If your jars are in the WEB-INF/lib folder of a web application ... 
> do you
> know in which sequence the application server adds those jars to the
> classpath ??

I don't understand. How will the answer to that question decide of the
correctness of the sentence "[...] different JVMs may process the 
classpath
in a different order" ?


Gilles

P.S. I don't know the answer to your question; if you know it, thanks 
for
      providing it.


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [Commons Wiki] Update of "MavenAndClasspath" by sebbapache

Posted by Gilles <gi...@harfang.homelinux.org>.
On Tue, 29 Jul 2014 11:44:56 +0100, sebb wrote:
> On 29 July 2014 11:12, Jörg Schaible <jo...@swisspost.com> 
> wrote:
>> Gilles wrote:
>>
>>>> [...]
>>>>
>>>> == Java classloader ==
>>>>
>>>> The Java runtime system only allows a single instance of a class 
>>>> to
>>>> exist in a given classloader.
>>>> Classes are uniquely identified using the package name and class
>>>> name.
>>>>
>>>> For example, the class {{{Utils}}} in the package
>>>> {{{org.apache.commons.example}}} can only appear once in a
>>>> classloader.
>>>>
>>>> The classloader uses the classpath to find the classes needed by 
>>>> an
>>>> application.
>>>>
>>>> If the classpath contains more than one jar which contains the 
>>>> above
>>>> Utils class, then the JVM will load only one of the instances.
>>>>
>>>> If the class instances in the different jars are identical, then 
>>>> this
>>>> is not a problem, but if there are two different versions of the
>>>> class, the classloader may choose the wrong one. And different 
>>>> JVMs
>>>> may process the classpath in a different order.
>>>
>>> Is that correct?
>>>
>>> Excerpt from
>>> 
>>> http://docs.oracle.com/javase/8/docs/technotes/tools/unix/classpath.html:
>>> -----
>>> The order in which you specify multiple class path entries is
>>> important.
>>> The Java interpreter will look for classes in the directories in 
>>> the
>>> order they appear in the class path variable.
>>> -----
>>>
>>> Gilles
>>>
>>>> [...]
>>
>> If your jars are in the WEB-INF/lib folder of a web application ... 
>> do you
>> know in which sequence the application server adds those jars to the
>> classpath ??
>
> No idea, but not really relevant to this issue, as the user has full
> control over which jars are in the folder.
>
> The issue with the Maven classpath is that the contents depend on the
> {gid, aid} pairs.
> Maven must be given the correct info.

What I was referring to is your description of the Java classpath 
processing:
the sentence seems to contradict the Oracle doc. [I.e. as you just 
said, when
the classpath is set, it's the first occurrence of a class that will be 
loaded,
independently (hopefully) of the JVM.]


Gilles


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [Commons Wiki] Update of "MavenAndClasspath" by sebbapache

Posted by sebb <se...@gmail.com>.
On 29 July 2014 11:12, Jörg Schaible <jo...@swisspost.com> wrote:
> Gilles wrote:
>
>>> [...]
>>>
>>> == Java classloader ==
>>>
>>> The Java runtime system only allows a single instance of a class to
>>> exist in a given classloader.
>>> Classes are uniquely identified using the package name and class
>>> name.
>>>
>>> For example, the class {{{Utils}}} in the package
>>> {{{org.apache.commons.example}}} can only appear once in a
>>> classloader.
>>>
>>> The classloader uses the classpath to find the classes needed by an
>>> application.
>>>
>>> If the classpath contains more than one jar which contains the above
>>> Utils class, then the JVM will load only one of the instances.
>>>
>>> If the class instances in the different jars are identical, then this
>>> is not a problem, but if there are two different versions of the
>>> class, the classloader may choose the wrong one. And different JVMs
>>> may process the classpath in a different order.
>>
>> Is that correct?
>>
>> Excerpt from
>> http://docs.oracle.com/javase/8/docs/technotes/tools/unix/classpath.html:
>> -----
>> The order in which you specify multiple class path entries is
>> important.
>> The Java interpreter will look for classes in the directories in the
>> order they appear in the class path variable.
>> -----
>>
>> Gilles
>>
>>> [...]
>
> If your jars are in the WEB-INF/lib folder of a web application ... do you
> know in which sequence the application server adds those jars to the
> classpath ??

No idea, but not really relevant to this issue, as the user has full
control over which jars are in the folder.

The issue with the Maven classpath is that the contents depend on the
{gid, aid} pairs.
Maven must be given the correct info.

> - Jörg
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [Commons Wiki] Update of "MavenAndClasspath" by sebbapache

Posted by Jörg Schaible <jo...@swisspost.com>.
Gilles wrote:

>> [...]
>>
>> == Java classloader ==
>>
>> The Java runtime system only allows a single instance of a class to
>> exist in a given classloader.
>> Classes are uniquely identified using the package name and class
>> name.
>>
>> For example, the class {{{Utils}}} in the package
>> {{{org.apache.commons.example}}} can only appear once in a
>> classloader.
>>
>> The classloader uses the classpath to find the classes needed by an
>> application.
>>
>> If the classpath contains more than one jar which contains the above
>> Utils class, then the JVM will load only one of the instances.
>>
>> If the class instances in the different jars are identical, then this
>> is not a problem, but if there are two different versions of the
>> class, the classloader may choose the wrong one. And different JVMs
>> may process the classpath in a different order.
> 
> Is that correct?
> 
> Excerpt from
> http://docs.oracle.com/javase/8/docs/technotes/tools/unix/classpath.html:
> -----
> The order in which you specify multiple class path entries is
> important.
> The Java interpreter will look for classes in the directories in the
> order they appear in the class path variable.
> -----
> 
> Gilles
> 
>> [...]

If your jars are in the WEB-INF/lib folder of a web application ... do you 
know in which sequence the application server adds those jars to the 
classpath ??

- Jörg


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [Commons Wiki] Update of "MavenAndClasspath" by sebbapache

Posted by Gilles <gi...@harfang.homelinux.org>.
> [...]
>
> == Java classloader ==
>
> The Java runtime system only allows a single instance of a class to
> exist in a given classloader.
> Classes are uniquely identified using the package name and class 
> name.
>
> For example, the class {{{Utils}}} in the package
> {{{org.apache.commons.example}}} can only appear once in a
> classloader.
>
> The classloader uses the classpath to find the classes needed by an
> application.
>
> If the classpath contains more than one jar which contains the above
> Utils class, then the JVM will load only one of the instances.
>
> If the class instances in the different jars are identical, then this
> is not a problem, but if there are two different versions of the
> class, the classloader may choose the wrong one. And different JVMs
> may process the classpath in a different order.

Is that correct?

Excerpt from 
http://docs.oracle.com/javase/8/docs/technotes/tools/unix/classpath.html:
-----
The order in which you specify multiple class path entries is 
important.
The Java interpreter will look for classes in the directories in the
order they appear in the class path variable.
-----

Gilles

> [...]



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org