Weird Question Regarding JVM and Packages

mark.yagnatinsky at barclays.com mark.yagnatinsky at barclays.com
Mon Apr 26 14:07:16 UTC 2021


That was a rather long email and I didn't really read the whole thing carefully, but as far as I can tell, what you're asking is this:
You have some third party dependency in package com.library.lib, or whatever, and you are not quite willing to fork this dependency.
Instead, you want to "tweak" a few of its classes.  In particular, you want to tweak com.library.lib.SessionStorage.
So you decided to create your very own class with that name, in the hope that Java will use your class instead of the "official one" and you're wondering if that will work.

Usually I use Stack Overflow for that kind of question, but since you've decided to ask it here...  The answer is it depends.
In the world of Java 9+ modules, that totally would not work, but I'm guessing you have no plans to convert this ancient lib into a module anyway.
So the real answer is: it depends on who goes first.  Suppose you launch java like this:

java -cp my-stuff.jar;old-lib.jar MyMainClass

Then it will work exactly as you hope.  Your class will have access to the package private members of the com.library.lib, since it is indeed a member of that package.
But suppose you instead run

java -cp old-lib.jar;my-stuff.jar MyMainClass

Then your class will never be loaded at all, since the one from old-lib will be found first.
If you're running in such a way as to make tweaking the class path inconvenient, you could actually remove the class SessionStorage from old-lib.jar.
(Probably best to make a backup copy of the jar just in case then.)

Hope that helps.  Or at least I hope that I didn't totally misunderstand your question.

_________________________________________________________________________________________________________________________________________________________________________________________________________________________________
“This message is for information purposes only, it is not a recommendation, advice, offer or solicitation to buy or sell a product or service nor an official confirmation of any transaction. It is directed at persons who are professionals and is not intended for retail customer use. Intended for recipient only. This message is subject to the terms at: www.barclays.com/emaildisclaimer.

For important disclosures, please see: www.barclays.com/salesandtradingdisclaimer regarding market commentary from Barclays Sales and/or Trading, who are active market participants; https://www.investmentbank.barclays.com/disclosures/barclays-global-markets-disclosures.html regarding our standard terms for the Investment Bank of Barclays where we trade with you in principal-to-principal wholesale markets transactions; and in respect of Barclays Research, including disclosures relating to specific issuers, please see http://publicresearch.barclays.com.”  
_________________________________________________________________________________________________________________________________________________________________________________________________________________________________
If you are incorporated or operating in Australia, please see https://www.home.barclays/disclosures/importantapacdisclosures.html for important disclosure.
_________________________________________________________________________________________________________________________________________________________________________________________________________________________________
How we use personal information  see our privacy notice https://www.investmentbank.barclays.com/disclosures/personalinformationuse.html 
_________________________________________________________________________________________________________________________________________________________________________________________________________________________________


More information about the jdk-dev mailing list