Search code examples
clojurenamespacesread-eval-print-loopleiningenpedestal

namespace not loaded in clojure pedestal


I am using the beginner guide in the pedestal guide but when trying to use use a namespace (require 'test) I get the following error messge: “Execution error (FileNotFoundException) at user/eval2012 (REPL:1). Could not locate test__init.class, test.clj or test.cljc on classpath.”

The same thing happens when trying (require 'hello)

I am using lein repl.

I have a directory called test and under src theres is a file called test.clj

test/src/test.clj:

(ns test
(:require [io.pedestal.http :as http]
[io.pedesteal.http.route :as route]))

test/src/hello.clj:

(defn respond-hello [request]
{:status 200 :body “Herllo world”})

any ideas?

test/deps.edn:

:deps                                                
 {io.pedestal/pedestal.service {:mvn/version "0.5.7"}
  io.pedestal/pedestal.route   {:mvn/version "0.5.7"}
  io.pedestal/pedestal.jetty   {:mvn/version "0.5.7"}
  org.slf4j/slf4j-simple       {:mvn/version "1.7.28"}}
 :paths ["src"]}

Solution

  • The clj repl differs from lein repl. To use lein repl, you need a project.clj file.

    I went through Pedestal's beginner guide successfully using the suggested clj, but I got your error when using lein repl:

    user=> (require 'test)
    Execution error (FileNotFoundException) at user/eval2006 (REPL:1).
    
    user=> (require 'hello)
    Execution error (FileNotFoundException) at user/eval2008 (REPL:1).
    Could not locate hello__init.class, hello.clj or hello.cljc on classpath.
    

    I looked at the difference between a clj project and a Leiningen project, and here's what I see:

    • clj uses deps.edn. Leiningen puts the dependencies in project.clj
    • clj has :paths ["src"]. Leiningen has :main and :target-path in project.clj

    So to switch from clj to lein repl, I added the project.clj file with this:

    (defproject pedestal "0.1.0-SNAPSHOT"
      :dependencies [[org.clojure/clojure "1.10.1"]
                     [io.pedestal/pedestal.service "0.5.7"]
                     [io.pedestal/pedestal.route "0.5.7"]
                     [io.pedestal/pedestal.jetty "0.5.7"]]
      :main ^:skip-aot test
      :target-path "target/%s")
    

    which follows my directory structure...

    .../pedestal/src/test.clj
    .../pedestal/project.clj
    .../...
    

    When I started it again, I didn't even need (require 'test) nor even(test/start). (start) did the trick, and the page would load

    Working webpage

    Leiningen differs from barebones clj tool. It points to starting files(?) differently and pulls in dependencies differently than a barebones clj project, which the guide recommended.

    From your question, I don't see mention of a project.clj, so maybe this is what you need.