Clojure Uberjar not loading resource file
Asked Answered
T

2

8

I use lein uberjar to create a standalone jar of an application.

When executing

java -jar dataloader-0.1.0-SNAPSHOT-standalone.jar,

it crashes with:

Caused by: java.lang.IllegalArgumentException: Not a file:
jar:file:dataloader-0.1.0-SNAPSHOT-standalone.jar!/configuration.json

I load the file via:

(ns dataloader.configuration
  (:gen-class)
  (:require [cheshire.core :refer :all]
            [clojure.java.io :as io]))

(def data-file
  (io/file
    (io/resource "configuration.json")))

project.clj

(defproject dataloader "0.1.0-SNAPSHOT"
  :description "Used for loading stage data into local vagrantbox"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :resource-paths ["resources"]
  :dependencies [[org.clojure/clojure "1.6.0"]
                 [clojurewerkz/elastisch "2.1.0"]
                 [org.clojure/java.jdbc "0.3.7"]
                 [mysql/mysql-connector-java "5.1.32"]
                 [clj-http "2.0.0"]
                 [org.clojure/data.json "0.2.6"]
                 [org.clojure/data.codec "0.1.0"]
                 [cheshire "5.5.0"]]

  :main ^:skip-aot dataloader.core
  :target-path "target/%s"
  :profiles {:uberjar {:aot :all}})

resources/configuration.json is put into the root folder of the jar

Troxler answered 26/8, 2015 at 17:10 Comment(0)
M
13

clojure.java.io/resource returns a URL, not a file. That's why you can call slurp on it. The error message is telling you that it's not a file, unfortunately it's not telling you that it's a URL.

Of course you could open the url with the java.net.URL api although that would be overkill in this case.

Michele answered 26/8, 2015 at 22:30 Comment(3)
I call slurp on the file afterwards. The problem is, that the file is not found. When developing, it works.Troxler
No, the error says "Not a file." It doesn't say "File not found." See this: clojuredocs.org/clojure.java.io/resourceMichele
You were right. I had to slurp directly on the resource.Troxler
G
5

If you want to read the content of the configuration.json file, do not call io/file. Instead, use slurp function, like that:

(def config (slurp (io/resource "configuration.json")))
Gunpowder answered 26/8, 2015 at 22:26 Comment(2)
I call slurp on the file afterwards. The problem is, that the file is not found. When developing, it works.Troxler
Is it possible for me to write to this configuration file after?Hypotonic

© 2022 - 2024 — McMap. All rights reserved.