Reputation: 1501
I am practising a Clojure tutorial "Basic Web Development" http://clojure-doc.org/articles/tutorials/basic_web_development.html
In the end, I am facing a problem. When I execute
$ java -jar target/clojure-webapp-0.1.0-SNAPSHOT-standalone.jar 8080
an error is occurring
Exception in thread "main" java.io.FileNotFoundException: 8080 (No such file or directory)
So the port number is parsed as a file name of a file that cannot be found.
I am not familiar with Java, but I think the situation is clear. The port number is a typical function parameter, not a file name. Please explain me it and tell how to fix it. More details:
s@lokal:~/Dropbox/clojure-webapp$ lein uberjar
Created /home/s/Dropbox/clojure-webapp/target/clojure-webapp-0.1.0-SNAPSHOT.jar
Created /home/s/Dropbox/clojure-webapp/target/clojure-webapp-0.1.0-SNAPSHOT-standalone.jar
s@lokal:~/Dropbox/clojure-webapp$ cat ~/Dropbox/clojure-webapp/src/clojure_webapp/handler.clj
(ns clojure-webapp.handler
(:require [clojure-webapp.views :as views]
[compojure.core :refer :all]
[compojure.route :as route]
[ring.adapter.jetty :as jetty]
[ring.middleware.defaults :refer [wrap-defaults site-defaults]])
(:gen-class))
(defroutes app-routes ; replace the generated app-routes with this
(GET "/"
[]
(views/home-page))
(GET "/add-location"
[]
(views/add-location-page))
(POST "/add-location"
{params :params}
(views/add-location-results-page params))
(GET "/location/:loc-id"
[loc-id]
(views/location-page loc-id))
(GET "/all-locations"
[]
(views/all-locations-page))
(route/resources "/")
(route/not-found "Not Found"))
(def app
(wrap-defaults app-routes site-defaults))
(defn -main
[& [port]]
(let [port (Integer. (or port
(System/getenv "PORT")
5000))]
(jetty/run-jetty #'app {:port port
:join? false})))
s@lokal:~/Dropbox/clojure-webapp$ java -jar target/clojure-webapp-0.1.0-SNAPSHOT-standalone.jar 8080
Exception in thread "main" java.io.FileNotFoundException: 8080 (No such file or directory)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:219)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:112)
at clojure.lang.Compiler.loadFile(Compiler.java:7449)
at clojure.main$load_script.invokeStatic(main.clj:278)
at clojure.main$script_opt.invokeStatic(main.clj:338)
at clojure.main$script_opt.invoke(main.clj:333)
at clojure.main$main.invokeStatic(main.clj:424)
at clojure.main$main.doInvoke(main.clj:387)
at clojure.lang.RestFn.applyTo(RestFn.java:137)
at clojure.lang.Var.applyTo(Var.java:702)
at clojure.main.main(main.java:37)
s@lokal:~/Dropbox/clojure-webapp$
Upvotes: 0
Views: 368
Reputation: 29984
This tutorial is a bit old, I believe. IMHO you might be happier using Pedestal instead of Ring anyway. You can find a "Hello World" tutorial here:
I have some working code that is the result of the tutorial, which you can clone from here:
[email protected]:cloojure/pedestal-tutorial.git
In a terminal window, start the code via:
> lein run
Then navigate your browser to:
http://localhost:8890/echo ; demo/test route
http://localhost:8890/greet ; generic greeting
http://localhost:8890/greet?name=Bob ; normal greeting
http://localhost:8890/greet?name=Voldemort ; unmentionable!
If you want to stick to Ring/Compojure, another option is the Clojure Cookbook, which has examples. It is available both in print (recommended) or online:
Here is another great book:
Upvotes: 1