idclark
idclark

Reputation: 958

Compojure app runs locally, but cannot find main class lein when deployed to Heroku

I have a small Clojure webapp built with ring and compojure. Although the webapp runs find locally on my laptop, when I push to Heroku the app crashes. The specific error from the logs is

 Starting process with command `java $JVM_OPTS lein ring server-headless 3000`
 app[web.1]: Error: Could not find or load main class lein
 app[web.1]: Picked up JAVA_TOOL_OPTIONS: -Xmx384m  -Djava.rmi.server.useCodebaseOnly=true
 heroku[web.1]: State changed from starting to crashed

My project.clj look like

(defproject hn-clj "0.1.1"
  :description "foo"
  :url "http://foo"
  :min-lein-version "2.0.0"
  :dependencies [[org.clojure/clojure "1.6.0"]
                 [compojure "1.3.1"]
                 [ring/ring-defaults "0.1.2"]
                 [clj-http-lite "0.2.0"]
                 [cheshire "5.4.0"]
                 [hiccup "1.0.5"]]
  :plugins [[lein-ring "0.8.13"]]
  :ring {:handler hn-clj.core.handler/app}
  :profiles
  {:dev {:dependencies [[javax.servlet/servlet-api "2.5"]
                        [ring-mock "0.1.5"]]}}
)

The entry-point for the application is in src/core/handler.clj

(ns hn-clj.core.handler
  (:require [compojure.core :refer :all]
            [compojure.route :as route]
            [compojure.handler :as handler]
            [ring.middleware.defaults :refer [wrap-defaults site-defaults]]
            [hn-clj.core.controllers.story :as story]
            [hn-clj.core.controllers.users :as users]
            ))

(defroutes app-routes
  (GET "/" [limit] (story/index limit))
  (GET "/stories/:id" [id] (story/show-story id))
  (GET "/users/:username" [username] (users/show username)))

(def app
  (wrap-defaults app-routes site-defaults))

Locally the app runs find with lein ring server-headless 3000 and in my Procfile I have put

web: java $JVM_OPTS lein ring server-headless 3000

Although I haven't created a main- function, this doesn't prohibit the app from running locally, I don't understand why the app won't run when deployed to Heroku. How should I refactor the handler.clj or the Procfile?

Upvotes: 2

Views: 789

Answers (1)

Nicolas Modrzyk
Nicolas Modrzyk

Reputation: 14187

Your procfile should be something like this:

 web: lein ring server-headless $PORT

To check your application can run properly on heroku, you can use a foreman port, which makes use of the procfile.

These days I am using gaffer, and the documentation on Procfile for Heroku is here.

Upvotes: 3

Related Questions