Fork me on GitHub
#leiningen
<
2020-03-24
>
tees20:03:10

I'm trying to include a binary executable in my uberjar so that I can shell out to it from within the uberjar. When running jar tvf, I can see that the binary is in the jar... but I can't get to the path of the binary when running the program. Anybody have any tips? I've only found stack over flow suggestions for reading in text files

tees20:03:04

(def PARSER-PATH (str
                  (.getPath ( "parser"))
                  "/bin/parser"))
This works when running a repl in cider, but my println returns this when running the uberjar with java:
"PARSER PATH IS ----------------- " "file:/Users/tees/Projects/firn/firn/target/uberjar/firn-0.1.0-SNAPSHOT-standalone.jar!/parser/bin/parser"

noisesmith20:03:49

@tees classpath resources can be loaded by the java process, but not by the OS

noisesmith20:03:52

at least not directly

noisesmith20:03:14

you need to write out the parser from the resource onto a filesystem before the OS will execute it

noisesmith20:03:50

@tees something like (io/copy (io/resource "parser/bin/parser") (io/file "/tmp/bin/parser"))

tees20:03:39

Thanks @noisesmith. Trying now.

noisesmith20:03:00

oh you'll need to flip the exec bit too (dunno if there's a way to do that with io/file...)

noisesmith20:03:13

(.setExecutable f true)

👍 4
tees20:03:24

Great, will give that a shot.

noisesmith20:03:50

nb don't use spit as that will try to do text-encoding stuff that will break the executable

tees20:03:20

Hmm, running into

1. Unhandled java.lang.IllegalArgumentException
   No method in multimethod 'do-copy' for dispatch value: [.URL
   .File]

tees20:03:25

when running the io/copy.

noisesmith20:03:50

try (io/input-stream (io/resource "parser/bin/parser"))

noisesmith20:03:02

I should have tested that

noisesmith20:03:07

(ins)user=> (io/copy (io/input-stream (io/resource "clojure/core.clj")) (io/file "core.clj"))
nil
(ins)user=> (count (slurp "core.clj"))
263653

noisesmith20:03:32

user=> (subs (slurp "core.clj") 0 100)
";   Copyright (c) Rich Hickey. All rights reserved.\n;   The use and distribution terms for this soft"

tees20:03:27

Works! Amazing. Thanks @noisesmith.