The thing is you’re learning and doing practice. In a real production scenario, you would know how your server is deployed and what will kick-start it on the host. Based on that, you’d know if you need to gen-class or not your -main entry point.
I’ll try and detail things a bit more.
First off, Clojure JVM is actually Java.
So in the world of Java and the JVM. If you want to create a program, you need a public Main class which has a public static main method on it. Once you have that, you can call java
on your Main class and the JVM will start and run the Java program.
java -cp classes myjavaapp.Main
A Clojure program IS a Java program. Thus to start a Clojure program you can do the same thing you’d do to start a Java program:
java -cp classes myclojureapp.Main
In which case, you need a way to create the Main class from Clojure. That’s what gen-class is for. So by adding a gen-class directive, when you compile your Clojure code, it will create a Java Main class.
Now, there’s another way to start a Clojure program, one that is Clojure specific and only works for Clojure apps, not for Java apps. That’s to use a Clojure based launcher like lein
or clj
. In those cases, you don’t need to use java
, though you still need a JVM installed. When you use those launchers instead of the standard java
launcher you don’t need a Java Main class. All you need is a Clojure namespace with a -main function in it. Which also means you do not need to have a gen-class.
clj -m myclojureapp
Or
; Given a project.clj file with :main myclojureapp defined
lein run
Will both similarly launch your Clojure program without needed a Java Main class, and thus you don’t need a gen-class.
If you intend to launch your app both with java
and with lein
or clj
, you will need to add the gen-class. It won’t conflict with lein
and clj
.