Skip to content

Instantly share code, notes, and snippets.

@zentrope
Last active April 22, 2016 07:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zentrope/f5530caf1fd96229bb89dc393d759812 to your computer and use it in GitHub Desktop.
Save zentrope/f5530caf1fd96229bb89dc393d759812 to your computer and use it in GitHub Desktop.
Find out the jar you're running in: Clojure
(ns engine.lib.utils
(:require
[clojure.string :as s]
[clojure.java.io :as io]))
;;-----------------------------------------------------------------------------
(def ^:private path-sep
(System/getProperty "path.separator"))
(def ^:private class-path
(System/getProperty "java.class.path"))
(defn- find-this-class
[]
(->> (str (namespace ::find-this-class) "__init")
(namespace-munge)
(symbol)
(resolve)))
;;-----------------------------------------------------------------------------
(defn find-jar-file
[]
(->> (.. (find-this-class)
getProtectionDomain
getCodeSource
getLocation
toURI)
(io/file)
(.getPath)))
(defn running-in-jar?
[]
(try
(.endsWith (s/lower-case (find-jar-file)) ".jar")
(catch Throwable t false)))
(defn clojure-jar
[]
(->> (-> class-path (s/split (re-pattern path-sep)))
(filterv #(.contains % "/clojure-1."))
(first)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment