aspose file tools*
The moose likes Java in General and the fly likes Same Class name & package name for 2 classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Same Class name & package name for 2 classes" Watch "Same Class name & package name for 2 classes" New topic
Author

Same Class name & package name for 2 classes

Mohammed Yousuff
Ranch Hand

Joined: Oct 17, 2007
Posts: 198
in my product, their are two classes in two different PROJECT with the Same Class name & package name. Based on the which class files order, the FIRST was getting loaded.....but another is not getting loaded ..... so i have to restart the server by changing the class path order every time....

Is there any way where we can have two classes can be loaded into the server with any issues???.... i heard some thing like you can keep two different class loaders which will loaded different classes... but not sure how can i do this for a application in weblogic server...

any lights on this thanks in advance ;)


My Thoughts : http://passion4java.blogspot.com
Try not to become a man of success but rather to become a man of value.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Sounds like a recipe for a perfect maintenance nightmare--I'd fix it at the source, rather than trying to work around it.

Barring that, if the "other" same class is in a separate jar I'd probably use a library manipulation tool to change its name.
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
are both project loaded in same JVM somehow (though I don't myself that whether this can be done or not? Someone please can help me at this) ? Then you can use classloader to load unload desired classes.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4637
    
    5

Mohammed Yousuff wrote:in my product, their are two classes in two different PROJECT with the Same Class name & package name.
Is there any way where we can have two classes can be loaded into the server with any issues???


Change one of the package names, add a subdirectory or something to make it unique.
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
Pat Farrell wrote:Change one of the package names, add a subdirectory or something to make it unique.


What if he is using jar files or has no direct access to those classes. Changing in existing directory structures, may affect other programs and more changes need to be done then.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4637
    
    5

Rahul.p Kumar wrote:What if he is using jar files or has no direct access to those classes. Changing in existing directory structures, may affect other programs and more changes need to be done then.


Yes, but I see no other reliable solution. If they are external jar files, shame on the vendor for using duplicate names. If you have source, change one.

Playing games with class loaders will work for a while, or rather may work for a while, but its not a long term reliable solution.
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
Pat Farrell wrote:If they are external jar files, shame on the vendor for using duplicate names.


Why shame? They are two different projects and may have same name by coincident or incident. The question put here is not very clear, though and I answered based on assumptions that let it be same name, now first question that myself do not know that how come two different project packages are conflicting each other? is it because they are in same jvm memory space ? (Then it becomes my question that whether it can be done ?). Second question is say it is done somehow, now is it ok to change the directory structures of respective projects or other measures should be taken up?
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4637
    
    5

Rahul.p Kumar wrote:Why shame? They are two different projects and may have same name by coincident or incident.


Because the naming convention is to include the name of the company in the package name.
org.apache.log4j.Logger; com.google.common.collect.ImmutableSet; com.sun.mail.dsn ... the list goes on and on.

The Internet domain namespace is unique on the whole Internet, essentially the whole world. This the package name within a company is completely under the control of the company.

There is no limit on the choice of names, or number of levels (perhaps some high limit, but none that matter), you can invent a project name and make it unique.

If you are too lazy to do that, you deserve to be publicly shamed.
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
Pat Farrell wrote:
If you are too lazy to do that, you deserve to be publicly shamed.

It is not matter of too lazy . Think about same company two projects, two teams and a coincident. Leave the shame word for the people for whom the end of the world is near by. Any type of mistake is possible and at any point of time, no matter how religiously you follow the standards.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4637
    
    5

Rahul.p Kumar wrote:It is not matter of too lazy . Think about same company two projects, two teams and a coincident.

Sorry, but I disagree with your justification. For a publicly released API, its unprofessional, as it is trivial to pick a unique name. Even huge global corporations do it properly. If your code is designed in one city, use the city in the package name, or your department.

For example, my old company could use com.cybercash.bangalore.set.... or com.cybercash.research....

If Google and Sun can do it, anyone who cares can do it.

Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
It's possible for two groups to independently come up with the same name at approximately the same time, yes. But at some point, those groups must become aware of each other. At that point, it is the moral responsibility of both groups to resolve the situation, and at least one of those groups must change their name. Typically, whoever chose their name first will keep it, and the second group must choose another. To do otherwise would be completely irresponsible.

If you've got a really big company, and somehow the name collision is not detected until one group or the other has moved on to another project... well, that's a sign of a very badly organized company.

A collision like this seems much more possible among open-source projects - jamón and JAMon come to mind. Both apparently started about the same time, and both probably would naturally have chosen "org.jamon" as the base of their package structure. But my guess is, jamón managed to register the domain first, and that was that - JAMon pretty much had to change to a different package name at that point. Because duplicating one already in use would be irresponsible.

Yes, in rare cases, coincidences may happen. But they need to get resolved.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4637
    
    5

Mike Simmons wrote:... managed to register the domain first, and that was that...Yes, in rare cases, coincidences may happen. But they need to get resolved.

The beauty of the Sun stylistic choice is that the issue of domain registration is already solved, and solved by someone not associated with Java. We can just use it. This makes it a previously solved problem, and we can just use the solution.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
Yep, exactly.

By the way, I'm just speculating on what happened with jamón vs JAMon. But the creator of JAMon, is a regular here, so maybe he'll offer his take. I note that jamon.com was taken by a commercial site, so maybe JAMon chose to keep the .com and change the JAMon part, becoming jamonapi.com, rather than keep the jamon and change the .com. Either way it's too bad, because JAMon makes a pretty cool name, what with the Rastafarian color scheme used at their site.
Jerome Pacleb
Greenhorn

Joined: Sep 11, 2006
Posts: 5
I have the same problem as Mohammed. I am using two libraries with the same name and package. Maven can compile them alright. Eclipse+m2eclipse cannot though.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
You are going to have to change the name of one package.
Jerome Pacleb
Greenhorn

Joined: Sep 11, 2006
Posts: 5
In the interest of time... Maybe later on...
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

You could load them in separate ClassLoaders, but in my opinion that way lies madness
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Same Class name & package name for 2 classes
 
Similar Threads
Class Loader - Applet
instanceof always returns false
"protected" access specifier
Simple Session bean in weblogic8.1
How to order the jars to be loaded by Classloaders in weblogic