3

I'm trying to run snake.clj from the Programming Clojure (3rd ed) book examples. Said file starts with this declaration:

(ns examples.snake
  (:import (java.awt Color Dimension) 
       (javax.swing JPanel JFrame Timer JOptionPane)
           (java.awt.event ActionListener KeyListener))
  (:require [examples.import-static :refer :all]))
(import-static java.awt.event.KeyEvent VK_LEFT VK_RIGHT VK_UP VK_DOWN)

; actual program omitted, see above link

Because I'm trying to run this on Windows, and the Clojure CLI Getting Started (recommended by the book) isn't yet available on Windows, I'm using leinigen instead. I've used these instructions to run:

  1. lein repl from the examples folder containing my snake.clj file
  2. => (load-file "snake.clj")

But this gives me an error:

CompilerException java.io.FileNotFoundException: Could not locate examples/import_static__init.class or examples/import_static.clj on class path...

Somehow my setup with a repl through leinigen fails to let me use import-static like that.

As a workaround, I've used WSL Ubuntu to install the Clojure CLI and run it in the root of the book's source code folder, loading the snake file directly using the suggested code in the book ((require '[examples.snake :refer :all])). That loads just fine, I can even run (game) from the loaded file, but of course that crashes because WSL Ubuntu has no GUI options (it crashes on an "No X11 DISPLAY..." error).

I would assume that the leinigen-based setup I used failed because I had to do (load-file "import_static.clj") first. In fact, doing so is a fine workaround because then it all works (after also doing (use examples.snake)), but this doesn't scale very well for multiple/recursive imports.

What is the proper way to use leinigen (on Windows) to run such scripts? Should I've created a leinigen project file? Or is there a repl-trick to do this?

Jeroen
  • 60,696
  • 40
  • 206
  • 339

1 Answers1

4

This isn't a direct answer to your question, but it'll help you get up and running. The problem is that the files aren't on the class path because the author seems have decided to neglect using projects altogether, so leiningen can't help you. The/A solution is to create a new project, and stick everything in there.

How to get it working:

  • Go to a directory, start up a command prompt there, and run lein new snake. This will create a "snake" folder containing the barebones structure of a project, complete with a "project.clj".

  • From the files you downloaded, copy the entire "src" folder (that contains the examples folder), and paste it into the new "snake" folder so it overwrites the generated "src". You should now have a path "snake/src/examples/".

  • Now that "snake/src" is on the class path, it can be referenced as expected. Go to the root "snake" folder, start up a command prompt, and run

    • lein repl
    • (require '[examples.snake :as s])
    • (s/game)

The game window should pop up as expected.

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
  • Thank you for your response. I will check a bit later if it works for me. (To be fair to the author, it has a `.edn` file or some such in the root of the codebase, to be used with the `clj` cli tool. It was my own (forced, because of W10) choice to use leiningen.) – Jeroen Mar 25 '18 at 15:34
  • @Jeroen I don't actually have any experience with any other build tools; I've never even seen a `deps.edn` file before. There may have been a way to get it working without using leiningen, but I can't speak for it. – Carcigenicate Mar 25 '18 at 15:40