4

I'm trying to run a short Clojure program in IntelliJ\Cursive. My program includes two files:

project.clj:

(defproject try3 "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [[org.clojure/clojure "1.7.0"]]
  :main try3.core)

and:

core.clj:

(ns try3.core)

(defn foo
  "I don't do a whole lot."
  [x]
  (println x "Hello, World!"))

(defn -main [] (println "hi"))

I want that when I run the project, the -main function will run automatically. What do I have to do to make this happen?

CrazySynthax
  • 13,662
  • 34
  • 99
  • 183
  • Have you checked [this page](https://cursive-ide.com/userguide/repl.html)? Your code looks ok. Fire up leiningen and `lein run` your project. – jmargolisvt Jan 30 '16 at 04:34

2 Answers2

1

I had to change the Run configuration in IntelliJ to get the program to print the Hello World string. Open Run > Edit Configuraitons... Add the test program if not done and make sure the configuration has the Run -main from Clojure namespace enabled.

IntelliJ screenshot

Andreas Guther
  • 422
  • 4
  • 7
  • I wonder why this has to be done manually for every single project. It's only natural to assume people will want to run their projects. Am I missing something? – royalstream May 25 '21 at 22:14
  • Maybe you know this already: Ctrl-Shift-R runs the currently open file. If your file has a main, there should be a green arrow next to it to conveniently run it. This will also create a temp run configuration that then can be saved. Right-click in the project navigation bar on the file will present some run options. – Andreas Guther May 31 '21 at 22:55
0

I assume that you want to create run configuration in IDEA. Also, -main method tells that you probably try to create runnable class. In order to achieve that you need to modify your namespace declaration:

(ns try3.core
  (:gen-class))
; note :gen-class above. It tells clojure that file is intended to 
; be compiled into class instead of be running as a script.

Then you need to create IDEA run configuration of Application type. Specify your namespace as main class. Take a look to the screenshot and comments in this tweet. Also you may need to modify "Compiler" > "Clojure Compiler" settings and set "Compile all Clojure namespaces" there. After these steps you should have runnable configuration.

Rorick
  • 8,857
  • 3
  • 32
  • 37