aspose file tools*
The moose likes Servlets and the fly likes order of class loading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "order of class loading" Watch "order of class loading" New topic
Author

order of class loading

J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 963
    
  13

I know the first response will be that there should only be one copy of a class file, but this is the mess I've inherited and I'm trying to get it cleaned up, so bear with me.

Let's say I have a class called IndexPage.class. One copy resides inside a jar file with lots of other classes, located in /WEB-INF/lib/myclasses.jar. Another copy, not in a jar but just a class file, resides in /WEB-INF/classes/com/my/package/. Which one will be loaded first?

I'm trying to figure out if I can "override" so to speak, the copy in the jar file with this stand-alone copy that contains some fixes not in the jarred version. I can update the jar of course, but I run the risk of losing my fixed version when the jar is replaced during deployment. Once the fixes are verified and checked in to CVS, then a new jar will be generated, but in the meantime, how can I be sure which one is being used?


"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
Stevens Miller
Ranch Hand

Joined: Jul 26, 2012
Posts: 523
    
    3

I was just running some experiments on this question yesterday. My results confirmed that you can use a .class to "override" (in the sense you mean it, I believe, rather than the Java meaning of the word) an entry in a .jar file if the .class file appears in a location specified in your classpath ahead of the .jar file.

So, if you have your .class in your home directory with your class containing main, and also your jar file, you can do this:



When it needs to load a class, java will look first in "." and find your .class file. If your .class files are elsewhere and/or your .jar files are elsewhere, just specify them accordingly, like this:



Again, java will look in the order you give to the -cp option. Pretty much like it was intended to do what you want. (And, fwiw, I don't think what you're doing is all that nutty; I often mess around with .class files before adding them to a .jar file that contains a previous version).
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 963
    
  13

Thanks for the info. Now I just have to figure out how the classpath is set for Tomcat. It looks like it's the common.loader property in the catalina.properties file, but I'm still digging around to make sure of that. I may have to jump over to the Tomcat forum to solve the rest of this.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7816
    
  21

Jk Robbins wrote:I'm trying to figure out if I can "override" so to speak, the copy in the jar file with this stand-alone copy that contains some fixes not in the jarred version. I can update the jar of course, but I run the risk of losing my fixed version when the jar is replaced during deployment. Once the fixes are verified and checked in to CVS, then a new jar will be generated, but in the meantime, how can I be sure which one is being used?

I hate to say, but this sounds like, rather than solving the problem, you're trying to live with it.

Now that may be OK as a short term Band-Aid; but if you agree that what you have is a "mess" (your own word), then fix it.

It's what my signature quote is all about.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Stevens Miller
Ranch Hand

Joined: Jul 26, 2012
Posts: 523
    
    3

Jk Robbins wrote:Thanks for the info. Now I just have to figure out how the classpath is set for Tomcat. It looks like it's the common.loader property in the catalina.properties file, but I'm still digging around to make sure of that. I may have to jump over to the Tomcat forum to solve the rest of this.

Yeesh, I sure can't help you with that.

Winston: I get the feeling that Jk is on the same page with your advice. Given his problem (he's inherited a .jar for which he apparently hasn't got the source, and wants to test a replacement .class without risking the loss of what he's already got) do you have an alternative approach you'd recommend? For example, one thing he could do might be to make a backup copy of his .jar (something he ought to be doing on general principle already) and then go ahead and replace the IndexPage.class file in that .jar with his own version. That has the advantage of letting him be completely sure which one he is running and not having to edit the classpath, but it might (depending on the larger context) force others to use his new version before he's ready to deploy it. Personally, I think the approach he's using is an attractive one, but I'm always open to your suggestions for better practices.
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 963
    
  13

I don't want to live with it, but over time my predecessors have installed .class files and the .jar file has gotten out of sync. I've tried to get an updated jar file from the dev team at our headquarters to no avail so I need to fix this on my own. I just want to make sure that I have the correct class files before I update the jar (after saving the original version of course).

For instance, last week I found a bug with a spectacular crash. A stack overflow brought the server to it's knees and we had to go to the backup server and pull the production server off the network to get it running again. HQ has sent me a "fixed" .class file, but I want to be absolutely sure it's fixed before I update the jar and move it into production.

And yes, the source for all the classes in that jar file are not available to me, so I'm muddling along and testing each class file carefully before manually updating the jar file.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61226
    
  66

As class loading for servlet containers is different than outside of the containers, I've moved this to the Servlets forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7816
    
  21

Stevens Miller wrote:Winston: I get the feeling that Jk is on the same page with your advice. Given his problem (he's inherited a .jar for which he apparently hasn't got the source, and wants to test a replacement .class without risking the loss of what he's already got) do you have an alternative approach you'd recommend? For example, one thing he could do might be to make a backup copy of his .jar (something he ought to be doing on general principle already) and then go ahead and replace the IndexPage.class file in that .jar with his own version. That has the advantage of letting him be completely sure which one he is running and not having to edit the classpath, but it might (depending on the larger context) force others to use his new version before he's ready to deploy it. Personally, I think the approach he's using is an attractive one, but I'm always open to your suggestions for better practices.

Well, plainly a backup is the first order of business.

But in general, I tend to prefer Adenauer's philosophy of 'year 0', rather than tentative prodding in situations like this:
Simply put: You have a mess. It needs to be cleaned up. Do it.

Unless I've misunderstood Jk's problem, it appears that he has duplicate classes that need to be rationalized. To me, introducing specific classpath orders, while it might work on a temporary basis, is adding to the problem, not solving it; and it also introduces more possibilities for getting things wrong.

If these classes are individual, you might be able to rationalize them one by one - and that may be as simple as eliminating any possible duplicates in favour of one that everybody agrees on - which could possibly be done without needing the source. If there are duplicated hierarchies, the problem obviously needs more analysis, but it still might simply come down to a choice of package. If there are duplicated implementations (ie, two classes with the same name but different behaviour), then clearly you have a major refactoring process ahead of you.

Best? Dunno. But when you've got a "mess", it seems to me that you need to fix it; and I don't think you do that by hedging your bets.

Oh, and whatever you do decide to do Jk - test, test, test.

Winston
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7816
    
  21

Jk Robbins wrote:I don't want to live with it, but over time my predecessors have installed .class files and the .jar file has gotten out of sync. I've tried to get an updated jar file from the dev team at our headquarters to no avail so I need to fix this on my own. I just want to make sure that I have the correct class files before I update the jar (after saving the original version of course).
...
And yes, the source for all the classes in that jar file are not available to me, so I'm muddling along and testing each class file carefully before manually updating the jar file.

I don't envy you your task, but you know what? When you're finished, you'll probably have an awful lot more expertise than most in dealing with exactly this sort of problem.

Also: Do you have javadocs for these classes? Because sometimes, they can be invaluable.

One other thing that's probably worth mentioning: Going through a process like this is a perfect opportunity for working out measures to stop it happening again: How did you get into this mess? Who (or what) is to blame? What procedures need to change?
This presumably is no small expense for your company, so I suspect they'll be interested in anything you have to say on the subject. An ounce of prevention is worth the 'pound of cure' that you're going through.

Winston
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 963
    
  13

Winston Gutkowski wrote:
Well, plainly a backup is the first order of business.

No argument there.

Winston Gutkowski wrote:
But in general, I tend to prefer Adenauer's philosophy of 'year 0', rather than tentative prodding in situations like this:
Simply put: You have a mess. It needs to be cleaned up. Do it.

I'm trying.

Winston Gutkowski wrote:
Unless I've misunderstood Jk's problem, it appears that he has duplicate classes that need to be rationalized. To me, introducing specific classpath orders, while it might work on a temporary basis, is adding to the problem, not solving it; and it also introduces more possibilities for getting things wrong.

I only asked about this for testing. They've sent me a class file that is supposed to fix the bug I found. So I want to test it, but if it fails, is it because the bug isn't fixed in the new class file, or because the container found the buggy version in the jar file first? You see my conundrum? I want to make sure that I'm testing what I think I'm testing. When I'm satisfied with each class file, I'll update the jar and delete all these orphan class files.

Winston Gutkowski wrote:
Oh, and whatever you do decide to do Jk - test, test, test.


You better believe it. I don't want another day like last Friday when the entire plant was down for 20 minutes while I frantically tried to get the backup server on line, only to discover that someone in HQ had installed new pages that weren't yet ready for production. Then it became a panic to get the primary server back online because the backup server was no longer a "real" backup server.

btw, I wonder if I can change my handle to J. Kevin or something. Everyone calls me Kevin but I had to pick JK because Kevin was already taken and nobody calls me James or Jim except the police and that makes me nervous. Jk sounds weird in the responses.


J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 963
    
  13

Winston Gutkowski wrote:
One other thing that's probably worth mentioning: Going through a process like this is a perfect opportunity for working out measures to stop it happening again: How did you get into this mess? Who (or what) is to blame? What procedures need to change? This presumably is no small expense for your company, so I suspect they'll be interested in anything you have to say on the subject. An ounce of prevention is worth the 'pound of cure' that you're going through.

Sorry, but I had to laugh at this. Phrases like "change management" and "rollback plan" might as well be Greek around here. And this is a huge company with plants all over the world. I'm pushing, but I'm up against a dinosaur of inertia. I think the best I can do is work with the system instead of fighting against it. I'm a very, very small fish in a huge ocean.

If it were up to me, I'd disable every account on the server except mine and that of my boss, and nobody would be allowed on that box without clearing it with me first and providing a report of EXACTLY what was changed afterward. But it's not a perfect world, and I don't have that kind of pull, so I do the best I can.
Stevens Miller
Ranch Hand

Joined: Jul 26, 2012
Posts: 523
    
    3

Winston Gutkowski wrote:
I don't envy you your task, but you know what? When you're finished, you'll probably have an awful lot more expertise than most in dealing with exactly this sort of problem.


On the other hand...

Robert Townsend wrote:

If you become an oustanding performer, your corporate reward may be a ticket to oblivion. Not intentionally. It's just because top managements, in between golf games, outside board meetings and charity drives, spend their time assigning their best people to problems instead of to opportunities. If you do a spectacular job with Time, therefore, you may be asked to save Life.


Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1059
    
  10

I must be missing something! The OP wants to replace a class file in a jar file so why is he not first backing up the jar file (as already suggested) and then updating the jar file (jar -u ); this way he knows he will be picking up the correct class file.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7816
    
  21

Jk Robbins wrote:Sorry, but I had to laugh at this. Phrases like "change management" and "rollback plan" might as well be Greek around here.
...
I think the best I can do is work with the system instead of fighting against it. I'm a very, very small fish in a huge ocean.

Then I would at least find out if you'll be given the time to write out the procedure(s) that you had to go through officially; and even if you're not, keep notes for yourself, because it sounds almost certain that you (or someone else) will have to do it again.

If it's official, and you can give it a nice pithy name like 'Project Redundant Crap', someone somewhere may get the message that this is a completely preventable waste of time and money.

Winston

[Edit] Actually, why not call it 'Project Wotam'? It's a nice Nordic-sounding name; and when someone from management asks why you called it that...
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7816
    
  21

Stevens Miller wrote:
Robert Townsend wrote: ...

Nice quote; but there's another way of looking at it - Isn't this how Red Adair made his millions?

There are a quite a few IT consultancy companies around that specialize in cleaning up other people's messes.

Winston
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 963
    
  13

Richard Tookey wrote:I must be missing something! The OP wants to replace a class file in a jar file so why is he not first backing up the jar file (as already suggested) and then updating the jar file (jar -u ); this way he knows he will be picking up the correct class file.


Simple answer, I didn't know there was an update switch. I rarely ever build a jar file. I've always had to unpack it, replace the desired file and repack it, which is always a pain. I was looking for an easy way to test before jumping through those hoops.

Thanks for the tip. Something new everyday.
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 963
    
  13

Winston Gutkowski wrote:
[Edit] Actually, why not call it 'Project Wotam'? It's a nice Nordic-sounding name; and when someone from management asks why you called it that...


I like that.

I've already had conversations with the people closest to this and we're developing some procedures so we can at least recover faster from something like this. I can't stop them from putting stuff on my server (it's MINE, dammit, leave it alone), but I can put some safety nets in place. The first is going to be a script that emails me when someone logs into one of my servers. I'm even considering using something like inotify or auditd to monitor file system changes.

All in all, it's just another learning experience.
Stevens Miller
Ranch Hand

Joined: Jul 26, 2012
Posts: 523
    
    3

Winston Gutkowski wrote:Actually, why not call it 'Project Wotam'? It's a nice Nordic-sounding name; and when someone from management asks why you called it that...

I love this. Reminds me, though, of a time back in the '80s (funny, how many things do that lately), when I was working for The Phone Company, and suggested to another programmer that we should ask for authorization to do SuchAndSuch. My co-worker said, "We could do that, but it would be both difficult and pointless." I thought about that for a moment, then asked him, "Aren't those the two main criteria for receiving project funding in this place?" He thought for a moment himself, then began writing the proposal for my idea.
Stevens Miller
Ranch Hand

Joined: Jul 26, 2012
Posts: 523
    
    3

Winston Gutkowski wrote:
Stevens Miller wrote:
Robert Townsend wrote: ...

Nice quote; but there's another way of looking at it - Isn't this how Red Adair made his millions?

There are a quite a few IT consultancy companies around that specialize in cleaning up other people's messes.

Winston

Ah, that's a brilliant insight! I would submit that the way Adair turned Townsend's "oblivion" into millions was to escape the "corporate reward" part, by being his own boss.

Jk, I think Winston has opened the door to your future destiny. Remember us, when you are great.
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 963
    
  13

Stevens Miller wrote:
Jk, I think Winston has opened the door to your future destiny. Remember us, when you are great.


Okay, when I make my first billion I'll rent out the Super Dome and we'll have a CodeRanch Festival.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: order of class loading