Determine which JAR file a class is from
To swiftly identify the source JAR of a class:
This line of code pinpoints the location where MyClass
took its first breath, leading you straight to its JAR residence.
Survival guide for bootstrap classes
When trying to retrieve the JAR location of core Java classes loaded by the bootstrap classloader, such as java.lang.String
, the getProtectionDomain()
method isn't helpful as it returns null
. In these cases, good old manual search or other tooling might be all you have.
Packing for diverse environments
In intricate applications, classes may be sourced from non-conventional locations: over http
, from a nested JAR, or via custom class loaders. The simplistic getCodeSource()
routine might fall short here. findPathJar()
from Lombok Patcher's LiveInjector can be a lifeline for these scenarios.
A peek inside the JAR
Unearth the secrets held within a JAR file or identify if a particular class resides within, using command-line magic:
All-purpose code for diverse paths
Here's an enhanced approach, much like a Swiss Army Knife, for any environment:
This approach caters to URI syntax exceptions and handles cases where the class loader turns shy.
Running a shell game for class hunters
You can automate your search for the elusive class hiding in some JAR file, with this handy shell script (findclass.sh
):
Supply the fully-qualified class name as a parameter, and the script will shake the bushes.
Conquering prodigious class packaging
Sometimes classes are packaged or nested in ways only a mad scientist could appreciate. Our conventional tools might feel a bit lost here.
Unpacking the Matryoshka
Nested classes stored in a JAR within a JAR scenario can leave traditional tools scratching their heads. Apache Commons Compress rolls up its sleeves for these nested archives.
Far-flung class loading
Distributed applications might fetch classes from a distant location. Here, getProtectionDomain().getCodeSource().getLocation() will usually return a URL looking something like http
, https
, or a variant depending on the tech stack.
Juggling dynamic classes
Modern frameworks and containers love to juggle dynamic classes, which can mystify your quest for class location discovery. Dynamic proxies won't be found lounging in a JAR, as they're whipped up on the go.
Was this article helpful?