Fork me on GitHub
#beginners
<
2016-12-03
>
mikepjb16:12:04

Doing some java interop here - a method requires the filepath of a font file

(defn font [property]
  (BaseFont/createFont
    (if (= (.toLowerCase ^String property) "helvetica")
      (BaseFont/HELVETICA)
      (.getPath (io/resource (str (.toLowerCase ^String property) ".ttf"))))
    (BaseFont/WINANSI)
    true))
which is fine when running with lein run - however after preparing a .jar with lein uberjar the path can't be resolved

mikepjb16:12:37

is there a way to get a filepath for resources stored in a jar?

mikepjb16:12:15

Google tells me the resources are stored on the classpath now if that helps

rauh16:12:10

@mikepjb What library/class is that? Usually they should all take URL returned from io/resource so just try omitting the .getPath

mikepjb16:12:31

iTextPdf 5

mikepjb16:12:45

thanks @rauh - I'll give that a go

rauh16:12:09

Looks like you can just pass in the path relative to the resource dir and it'll load it. Obviously untested

mikepjb16:12:56

you're right @rauh

(defn font [property]
  (BaseFont/createFont
    (if (= (.toLowerCase ^String property) "helvetica")
      (BaseFont/HELVETICA)
      (str (.toLowerCase ^String property) ".ttf"))
    (BaseFont/WINANSI)
    true))

mikepjb16:12:10

This works fine for both uberjar and lein run

mikepjb16:12:17

thanks for the help!

rauh16:12:49

You're welcome. In general: Dont assume io/resource returns a file. Most APIs take URLs

rauh16:12:20

I've made that mistake like many others 🙂

justinlee23:12:23

how do I read a field from a record? ex: (defrecord Data [id]) and then (:id data-item) doesn't work

justinlee23:12:07

oh wait, defining a single record just worked. maybe I'm doing something else wrong.

chokheli23:12:02

Hi Clojurians, how can I resolve dependencies without restarting cider?