基于Leiningen的REPL加载clj文件:Windows下运行Programming Clojure的snake.clj示例遇阻
Hey there, let's walk through how to get that snake example up and running with Leiningen's REPL on Windows. Here's the step-by-step breakdown:
1. 确认Leiningen已安装
First, make sure you have Leiningen properly set up on your Windows machine. Verify it works by opening a command prompt and running:
lein --version
If this throws an error, install Leiningen first before proceeding.
2. 创建Leiningen项目
Open your command prompt and run these commands to generate a standard Clojure project structure:
lein new app snake-example cd snake-example
3. 配置项目文件与命名空间
The snake example uses the examples.snake namespace and depends on a custom examples.import-static macro from the book. Let's set up the required files:
- Inside the
srcfolder, create a subfolder namedexamples - In
src/examples, createsnake.cljand paste your full snake code (including the namespace declaration and import lines you shared) - Also in
src/examples, createimport_static.cljwith this content (this is the custom macro the book uses):
(ns examples.import-static) (defmacro import-static [class & fields] `(do ~@(map (fn [field] `(def ~(symbol field) (. ~class ~(symbol field)))) fields)))
4. 启动Leiningen REPL
From the root of your snake-example project, run this command to start the REPL:
lein repl
Wait a moment for the REPL to initialize—you'll see a prompt like snake-example.core=> when it's ready.
5. 加载并运行蛇形程序
Once in the REPL, load the snake namespace with:
(require '[examples.snake :as snake])
If you edit the code later and need to reload it, use:
(require '[examples.snake :as snake] :reload)
Now run the program. If your snake.clj includes a -main function (standard for Clojure apps), execute:
(snake/-main)
If the example uses a custom start function (like a start function that initializes the Swing window), call that instead—e.g., (snake/start).
Windows特有的小提示
- After the snake window pops up, click on it to give it keyboard focus—otherwise, the arrow keys won't control the snake.
- If you get class-not-found errors, double-check your namespace declarations and imports for typos—small mistakes here are a common culprit!
内容的提问来源于stack exchange,提问作者Jeroen

