Bug 63970 - java.lang.ClassCastException: org.apache.catalina.webresources.CachedResource$CachedResourceURLConnection cannot be cast to java.net.JarURLConnection
Summary: java.lang.ClassCastException: org.apache.catalina.webresources.CachedResource...
Alias: None
Product: Tomcat 8
Classification: Unclassified
Component: Catalina (show other bugs)
Version: 8.5.49
Hardware: PC Mac OS X 10.1
: P2 normal (vote)
Target Milestone: ----
Assignee: Tomcat Developers Mailing List
Depends on:
Reported: 2019-11-27 15:17 UTC by Dmitry Treskunov
Modified: 2019-11-28 20:04 UTC (History)
0 users


Note You need to log in before you can comment on or make changes to this bug.
Description Dmitry Treskunov 2019-11-27 15:17:13 UTC
The following code starts throwing a ClassCastException after upgrading to Tomcat 8.5.49:

URL url = this.getClass().getResource("/preset.propetries");
String scheme = url.getProtocol();
if ("jar".equals(scheme)) {
      JarURLConnection con = (JarURLConnection) url.openConnection();

I guess it happens because of this commit https://github.com/apache/tomcat/commit/03e7bc8487cb706adf1f56586948a7762dd42d14#diff-b718dcafc40f187cc9aaa1518e63033d
Comment 1 Mark Thomas 2019-11-27 19:25:08 UTC
Yes, this is caused by that commit.

The fix looks fairly simple but we are at the limit of what the current implementation can reasonably do in terms of guaranteeing consistency. Chances are you are going to end up accessing the resource directly, bypassing the cache and running the risk of seeing inconsistent results. That said, the chances of a resource inside a JAR changing should be a lot lower.
Comment 2 Dmitry Treskunov 2019-11-27 19:39:10 UTC
Thanks for the answer. Sorry, I didn't quite understand, does it mean that you are not going to fix it?
Comment 3 Mark Thomas 2019-11-27 22:16:45 UTC
Don't worry - I've already fixed it. I was just waiting for a couple of test builds to upload so I could point you towards them for testing. I'd like to try and avoid further regressions in the next round of releases.
Comment 4 Mark Thomas 2019-11-27 22:32:58 UTC
OK. The upload is complete. The dev build is here:

If you could download, test and report back that would be very helpful.

- this is NOT an official release
- this is NOT for production use
- this is ONLY for testing this bug
- please don't blame me if your server catches fire during testing ;)

(Sorry to go on about this but ASF releases have various bits of QA and legal machinery behind them whereas test/dev builds like this don't and we need to make sure folks reading this understand there is a difference.)
Comment 5 Dmitry Treskunov 2019-11-28 12:41:53 UTC
Hi, thanks for the build. We are using the 8.5.x version, could you please backport the fix there? Otherwise, I can't test it
Comment 6 Mark Thomas 2019-11-28 13:36:27 UTC
Already done. Test build here:
Comment 7 Dmitry Treskunov 2019-11-28 18:19:02 UTC
Hi, thanks, it works! 
Do you have any estimates for the 8.5.50 release?
Comment 8 Mark Thomas 2019-11-28 18:52:11 UTC
I'm expecting to start the release early next week so it should be available towards the end of next week / beginning of the following week.
Comment 9 Dmitry Treskunov 2019-11-28 19:18:17 UTC