[英]Could not locate proj/core.clj on classpath when calling clojure from java
[英]Could not locate apply /clojure/core/vector__init.class or apply/clojure/core/vector.clj on classpath
我嘗試運行程序時遇到以下錯誤: Exception in thread "main" java.io.FileNotFoundException: Could not locate apply /clojure/core/vector__init.class or apply/clojure/core/vector.clj on classpath: , compiling:(erbium/compile.clj:1:1)
。 它似乎指向下面的文件,並建議我需要在我的依賴項中放入clojure.core/vector
。 默認情況下不包括嗎?
(ns erbium.compile
(require `[clojure.string :as string])
)
(defn produce-out "Convert 'command %1 %2 %3' with stack [5 6 7] to 'command 5 6 7'" [word stack definitions]
(let [
code (definitions word) ; dictionary/hash lookup. eg. "println" -> "echo $1"
replacement (fn [match] (-> match second Long/parseLong dec stack str))
]
; evaluate arguments. eg. "echo %1"
; stack=["blah"]
; -> "echo blah"
(string/replace code #"%(\d)" replacement)
)
)
(defn parse-word "Verifies that word is in defintitions and then returns (produce-out word stack)" [word stack definitions]
(if (some #{word} (keys definitions))
(produce-out word stack)
)
)
(defn compile "Main compile function" [code]
(let [
split-code (string/split code #"\s")
definitions {
"println" "echo %1"
"+" "%1 + %2"
"-" "%1 - %2"
}
stack []
]
(for [word [split-code]]
(if (integer? (read-string word))
(do
(println "Found integer" word)
(def stack (conj stack (read-string word)))
(println "Adding to argument stack:" stack)
)
; else
(do
(parse-word word stack definitions)
(def stack [])
)
)
)
)
)
如果有所不同,則此文件由核心文件通過(load "compile")
。
我看到的第一個錯誤是:
(require `[clojure.string :as string])
應該是這樣的:
(:require [clojure.string :as string])
在常規clojure源文件中。 這為我解決了。
也就是說,這里有一些一般性建議:
split-code (str/split code #"\\s")
不能按您的要求運行[clojure.string :as string]
因此將其更改為: split-code (string/split code #"\\s")
為了擴大答案,“ require”出現在名稱空間聲明“ ns”中。 ns
實際上是一個宏,它擴展為一系列語句以創建名稱空間並進行一些設置。
該宏將(:require ...)
類的語句視為對名稱為require
的函數的調用,並自動引用以下任何參數。 由於您自己指定了報價:
(ns erbium.compile
(require '[clojure.string :as string]))
然后結果最終被雙引號引起,對require
的調用最終成為:
... (require (quote (quote [clojure.string :as string])))
因此,最終會嘗試加載一個名為“ quote”的名稱空間,然后加載一個語法錯誤的向量。 :)
ns
宏是設置名稱空間的一種標准且便捷的方法,但是花了我很長時間才能正確學習它。 我發現最好的方法是復制其他人的設置代碼,直到我了解如何正確執行它為止。
順便說一句,雖然標准是使用:require
所以使用require
代替:require
並不重要,因此它看起來並不像對該函數的直接調用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.