skip to Main Content

i started learning clojure, and i have one problem.
I just have 2 files, project.clj

(defproject test-clojure-project "0.1.0-SNAPSHOT"
  :description "A simple Clojure web project"
  :dependencies [[org.clojure/clojure "1.10.0"]
                 [ring "1.10.0"]
                 [ring/ring-jetty-adapter "1.10.0"]
                 [ring/ring-json "0.5.0"]
                 [cheshire "5.10.0"]
                 [org.postgresql/postgresql "42.3.1"]
                 [org.clojure/java.jdbc "0.7.11"]
                ;;  [ring/ring-reload "0.3.1"]
                 ]
  :main ^:skip-aot test-clojure-project.core
  :target-path "target/%s"
  :profiles {:uberjar {:aot :all}})

and src/test-clojure-project/core.clj`

(ns test-clojure-project.core
  (:require [ring.adapter.jetty :as jetty]
            [ring.util.response :as response]
            [ring.middleware.reload :refer [wrap-reload]]
            [cheshire.core :as json]
            [clojure.java.jdbc :as jdbc]
            [clojure.java.jdbc.ddl :as ddl]))

(def db-spec
  {:subprotocol "postgresql"
   :subname "//localhost/test_db"
   :user "postgres"
   :password "secret"})

(def posts-table
  [:posts
   [:id :serial "PRIMARY KEY"]
   [:content :text]])

(defn create-post [post]
  (jdbc/with-connection db-spec
    (jdbc/insert! :posts
                  {:content (:content post)})))

(defn get-data []
  (jdbc/with-connection db-spec
    (jdbc/query "SELECT * FROM posts")))

(defn handler [request]
  (let [uri (:uri request)]
    (cond
      (= uri "/posts-list")
      (let [response-data (get-data)]
        (response/response (str response-data)))

      (= uri "/create-post")
      (if (= (:request-method request) :post)
        (let [json-data (-> request :body slurp)
              parsed-data (json/parse-string json-data true)
              created-post (create-post parsed-data)]
          (response/response (json/generate-string created-post)))
        (response/response "Invalid request method"))

      :else
      (response/response "Route not found"))))

(defn -main []
  (do
    (println "Server started successfully on port 3000")
    ;; Create the 'posts' table if it doesn't exist
    (jdbc/with-connection db-spec
      (ddl/create-table-ddl posts-table :if-not-exists))
    (jetty/run-jetty (wrap-reload handler) {:port 3000})))

Am getting this error

2023-08-05 17:41:30.332:INFO::main: Logging initialized @1359ms to org.eclipse.jetty.util.log.StdErrLog
Exception in thread "main" Syntax error compiling at (test_clojure_project/core.clj:1:1).
Caused by: java.io.FileNotFoundException: Could not locate clojure/java/jdbc/ddl__init.class, clojure/java/jdbc/ddl.clj or clojure/java/jdbc/ddl.cljc on classpath.

how can i fix it can somone help.

2

Answers


  1. I see two problems affecting ns loading:

    1. Clojure uses munge to remove special characters from identifiers, like namespace names before looking up the corresponding resource. For example - is replaced by _.

    Therefore, your source file should be in a folder named test_clojure_project instead.

    1. There is no clojure.java.jdbc.ddl namespace defined in org.clojure/java.jdbc. Remove the require form and use just jdbc/create-table-ddl.
    Login or Signup to reply.
  2. There are a number of problems here. As Erdos pointed out, clojure.java.jdbc.ddl doesn’t exist — create-table-ddl is part of clojure.java.jdbc. And, yes, your file should be src/test_clojure_project/core.clj for a ns value of test-clojure-project.core.

    You should remove [clojure.java.jdbc.ddl :as ddl] (but you still need [clojure.java.jdbc :as jdbc] since you are using functions from that).

    In addition, the code you have looks like it needs a much older version of org.clojure/java.jdbc — in version 0.7.11 there is no with-connection and functions like insert! must be passed a db-spec as their first argument.

    See https://cljdoc.org/d/org.clojure/java.jdbc/0.7.11/api/clojure.java.jdbc#insert! for the 0.7.11 docs (and note that it is not the most recent version).

    Whatever tutorial or book you are learning from is outdated. Did you try using the exact versions for dependencies in project.clj that the tutorial/book said to use?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search