Printing the class-path in Clojure

Let’s compare how we print the class-path in Clojure and how we do it on Java.

In Clojure:

(println (seq (.getURLs (java.lang.ClassLoader/getSystemClassLoader))))

In Java:


public class PrintClasspath {
    public static void main(String[] args) {
        //Get the System Classloader
        ClassLoader sysClassLoader = ClassLoader.getSystemClassLoader();

        //Get the URLs
        URL[] urls = ((URLClassLoader)sysClassLoader).getURLs();

        for(int i=0; i< urls.length; i++)

To be fair, the output is not the same, but the effect is.

9 thoughts on “Printing the class-path in Clojure

  1. This might not work in all cases.
    You could also want:

    (import 'clojure.pprint)
    (pprint (seq (.getURLs (->  (Thread.currentThread)  (.getContextClassLoader)))))
    1. Sorry:

      (use 'clojure.pprint)
      (import 'java.lang.Thread)
      (pprint (seq (.getURLs (-> (Thread/currentThread) (.getContextClassLoader)))))
  2. this code worked fine with the older version of clojure. I tried it with the newer one but unfortunately, it didn’t work out. What could be the issue??

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.