Reputation: 543
I'm a bit of a Clojure novice, but I don't understand what's wrong about my code.
The code bellow to my knowledge should set img
to a new BufferedImage
object.
(import javax.imageio.ImageIO)
(import java.io.File)
(def img (ImageIO/read (File. "C:\\input.png")))
However, when I execute lein run
I get this exception:
Exception in thread "main" java.lang.RuntimeException: No such namespace: ImageIO, compiling:(fstego/core.clj:8:14)
Upvotes: 1
Views: 715
Reputation: 14187
Not sure how you can run
lein run
without a -main method, so here is some sample code:
project.clj
(defproject testi "0.1.0-SNAPSHOT"
:main fstego.core
:dependencies [[org.clojure/clojure "1.6.0"]])
src/fstego/core.clj
(ns fstego.core
(:import [javax.imageio ImageIO])
(:import [java.io File]))
(defn -main[& args]
(if-let [ path (first args) ]
(let[ img (ImageIO/read (File. path))]
(println
"Input Image has the following dimensions: "
(.getWidth img) "x" (.getHeight img)))
(println "No image")))
And then
lein run <path_to_image>
Should output something like:
Input Image has the following dimensions: 251 x 60
Edit Why was the java import not found when running lein run?
Before running the main method of your namespace, the compilation phase is executed and without the specific directive, your namespace is compiled ahead of time (called aot), to Java. To disable the feature, you need to add this to your project.clj
:main ^:skip-aot fstego.core
If you clean up the target folder and execute lein run again the import will be found wherever you add them.
If you do not specify skipping this task, or if you want to call your main method directly from a jar file, the lifecycle is different.
Running the namespace means the code is executed just as it would be in the REPL, thus the import calls are executed, and the referenced classes are found.
Running the main method straight from the compiled bytecode means the call to import is not included in the bytecode, thus ignored, thus the call to ImageIO fails to find the class properly.
Upvotes: 1