The idea of classpath is to hide where classes come from. You may have classes with the same name loaded from different classloaders, you may have the same class in multiple jars and rely on classpath ordering to choose the correct one.
Why do you want to know? If it's for any other reason than debug/logging purposes you are on dangerous ground and should tread carefully.
In fact it's perfectly reasonable for classes to have no jar file. This can happen in java for any runtime generated classes (think proxies).
In clojure a simple example would be as shown in the repl session below... You'll see @mikera's suggestion works fine for clojure.lang.Atom
which is a built in class. But when you use a deftype
to create your own type, clojure generates a class and it has no location...
user> (prn (-> clojure.lang.Atom
(.getProtectionDomain)
(.getCodeSource)
(.getLocation)))
#<URL file:/workspace/clj-scratch/lib/clojure-1.3.0.jar>
nil
user> (deftype Foo [])
user.Foo
user> (prn (-> (Foo.)
(.getClass)
(.getProtectionDomain)
(.getCodeSource)
(.getLocation)))
nil
nil
user>