Clojure Couchbase Dependency Issue - emacs

New to Clojure and have been using Leiningen to manage my Clojure project.
I want to connect to a Couchbase bucket so I included:
[couchbase-clj "0.2.0"]
in my project.clj as stated in the GitHub Repo https://github.com/otabat/couchbase-clj
I also ran:
lein deps
to install my dependency, although the Leiningen guide states it will download the dependency
automatically if I don't. https://github.com/technomancy/leiningen/blob/stable/doc/TUTORIAL.md#dependencies
So I have to following code in my core.clj
(ns first-app.core
(:gen-class :main true ))
(:require [couchbase-clj.client :as c]))
(c/defclient client {:bucket "subgate"
:uris ["http://127.0.0.1:8091"]})
(defn -main
"I don't do a whole lot ... yet."
[& args]
(println "Hello, World! "))
But when I run:
lein run
I get the error:
Exception in thread "main" java.lang.ClassNotFoundException: couchbase-clj.client, compiling:(first_app/core.clj:4:3)
Here is my dependency tree:
[cider/cider-nrepl "0.8.1"]
[cljs-tooling "0.1.3" :exclusions [[org.clojure/clojure]]]
[compliment "0.2.0" :exclusions [[org.clojure/clojure]]]
[org.clojure/java.classpath "0.2.0" :exclusions [[org.clojure/clojure]]]
[org.clojure/tools.namespace "0.2.5" :exclusions [[org.clojure/clojure]]]
[org.clojure/tools.trace "0.7.8" :exclusions [[org.clojure/clojure]]]
[org.tcrawley/dynapath "0.2.3" :exclusions [[org.clojure/clojure]]]
[clojure-complete "0.2.4" :exclusions [[org.clojure/clojure]]]
[couchbase-clj "0.2.0"]
[com.couchbase.client/couchbase-client "1.3.2"]
[commons-codec "1.5"]
[io.netty/netty "3.5.5.Final"]
[net.spy/spymemcached "2.10.5"]
[org.apache.httpcomponents/httpcore-nio "4.3"]
[org.apache.httpcomponents/httpcore "4.3"]
[org.codehaus.jettison/jettison "1.1"]
[stax/stax-api "1.0.1"]
[org.clojure/data.json "0.2.4"]
[org.clojure/clojure "1.8.0"]
[org.clojure/tools.nrepl "0.2.12" :exclusions [[org.clojure/clojure]]]
Any tips are welcome as I am new to Clojure!
Thanks In advance.

Had to include my require before the ns delimiter.
(ns first-app.core
(:gen-class :main true )
(:require [couchbase-clj.client :as c]))
Special thanks to moxaj on Reddit

Related

UTF-8 symbols not displaying properly in browser using Pedestal

I've made simplest Pedestal project and run it in my repl locally. However, after checking browser at localhost:8890 I saw � (replacement characters) instead of actual text (cyrillic symbols) I put in my Pedestal route.
I also checked in browser devtools response headers:
Content-Type:text/html;charset=utf-8 is present.
Before you ask:
Yes, I set up charset=UTF-8 in response, as you can see in code below.
My core.clj file is also in UTF-8 encoding.
I've tried other browsers as well, same thing.
Additional info: I'm using Windows, but have never encountered this issue before with other libraries and frameworks (ring, yada). Could it be that Pedestal somehow corrupts my code internally while passing it to jetty server? I dunno.
The whole code of project:
(ns samplepedestal.core
(:require [io.pedestal.http :as http]
[io.pedestal.http.route :as route])
(:gen-class))
(defn html-response
[req]
{:status 200
:body "<html lang=\"ru\">
<head>
<meta charset=\"utf-8\" />
<title>Текст на русском</title>
</head>
<body>Текст на русском</body>
</html>"
:headers {"Content-Type" "text/html; charset=UTF-8"}})
(def routes
(route/expand-routes
[[["/" {:get `html-response}]]]))
(def service-map
{::http/routes routes
::http/type :jetty
::http/port 8890})
(defn start []
(http/start (http/create-server service-map)))
;; -- Interactive development
(defonce server (atom nil))
(defn start-dev []
(reset! server
(http/start (http/create-server
(assoc service-map
::http/join? false)))))
(defn stop-dev []
(http/stop #server))
(defn restart []
(stop-dev)
(start-dev))
;; ---
(defn -main
"I don't do a whole lot ... yet."
[& args]
(println "Hello, World!"))
This is kind of strange behaviour, I have no clue what I'm missing, so any help would be appreciated. Thanks!
I think the problem is in how REPL starts. Have you got something along the lines of
Starting nREPL server... "C:\Program
Files\Java\jdk1.8.0_66\jre\bin\java" -Dfile.encoding=Cp1251
-XX:-OmitStackTraceInFastThrow -Dclojure.compile.path=D:\workspace-clojure\the-next-big-server-side-thing\target\classes
-Dthe-next-big-server-side-thing.version=0.0.1-SNAPSHOT -Dclojure.debug=false -Didea.launcher.port=50071 "-Didea.launcher.bin.
when REPL starts?
If so, you might want to add new JVM parameter to fix that.

How should I configure Vulkan for cider-jack-in?

I'm working through the newly released Vulkan Tutorial in Clojure with CIDER, and I've hit a bit of a snag. The example makefile project works perfectly, but I'm having trouble translating it into Clojure.
My build.boot file just specifies the :source-paths and adds LWJGL as a dependency:
(set-env!
:source-paths #{"src"}
:dependencies
(let [lwjgl-version "3.0.0"]
[['org.lwjgl/lwjgl lwjgl-version]
['org.lwjgl/lwjgl-platform lwjgl-version :classifier "natives-linux"]]))
Then, in src/example/core.clj, I have an extension-count function that uses vkEnumerateInstanceExtensionProperties as demonstrated in the original example:
(ns example.core
(:import (org.lwjgl.vulkan VK10)))
(defn extension-count []
(let [^String layer-name nil
property-count (int-array 1)]
(VK10/vkEnumerateInstanceExtensionProperties layer-name property-count nil)
(first property-count)))
Now, from Bash, I can set the relevant environment variables LD_LIBRARY_PATH and VK_LAYER_PATH as I start up a REPL:
$ VULKAN_SDK_PATH=~/VulkanSDK/1.0.21.1/x86_64 LD_LIBRARY_PATH=$VULKAN_SDK_PATH/lib VK_LAYER_PATH=$VULKAN_SDK_PATH/etc/explicit_layer.d boot repl
boot.user=> (require '[example.core :refer [extension-count]])
nil
boot.user=> (extension-count)
4
As you can see, everything works correctly. But of course, when I use cider-jack-in by C-c M-j instead, I get an UnsatisfiedLinkError because CIDER isn't setting those variables:
boot.user> (import (java.util.function Consumer)
(org.lwjgl.system Configuration))
org.lwjgl.system.Configuration
boot.user> (Configuration/setDebugStreamConsumer
(reify Consumer
(accept [_ message]
(println message))))
nil
boot.user> (require '[example.core :refer [extension-count]])
nil
boot.user> (extension-count)
[LWJGL] Failed to load a library. Possible solutions:
a) Set -Djava.library.path or -Dorg.lwjgl.librarypath to the directory that contains the shared libraries.
b) Add the JAR(s) containing the shared libraries to the classpath.
[LWJGL] Enable debug mode with -Dorg.lwjgl.util.Debug=true for better diagnostics.
java.lang.UnsatisfiedLinkError: Failed to locate library: libvulkan.so.1
Am I supposed to be setting java.library.path or org.lwjgl.librarypath, as suggested in the above error message, instead of LD_LIBRARY_PATH? I can set either of those variables from profile.boot:
(System/setProperty
"java.library.path"
(str (System/getProperty "user.home") "/VulkanSDK/1.0.21.1/x86_64/lib"))
Now when I try C-c M-j again, it works:
boot.user> (require '[example.core :refer [extension-count]])
nil
boot.user> (extension-count)
4
However, this still doesn't let me set VK_LAYER_PATH, which will be fairly important in the future:
We will start using validation layers in Vulkan and you need to tell the Vulkan library where to load these from using the VK_LAYER_PATH variable:
test: VulkanTest
LD_LIBRARY_PATH=$(VULKAN_SDK_PATH)/lib VK_LAYER_PATH=$(VULKAN_SDK_PATH)/etc/explicit_layer.d ./VulkanTest
How can I set these environment variables for cider-jack-in? I'd prefer not to have to manually configure CIDER's dependencies for a standalone repl in a separate terminal and then connect to it using cider-connect, but if there's no other option here, I guess that's what I'll have to do.

Clojure server-socket ClassNotFoundException error

I'm trying to test mire.
When I execute this code
(ns mire
(:use [clojure.contrib server-socket duck-streams]))
I have java.io.FileNotFoundException error.
Googling to find that clojure.contrib is deprecated, I added server-socket as dependencies in the project.clj, and executed leon repl to install the jar libraries
mire> lein repl
Retrieving server-socket/server-socket/1.0.0/server-socket-1.0.0.pom from clojars
Retrieving server-socket/server-socket/1.0.0/server-socket-1.0.0.jar from clojar
Then in the emacs/cider, I tried to use this code to get the same FileNotFoundException error.
(ns mire
(:use [server.socket server-socket duck-streams]))
java.io.FileNotFoundException: Could not locate server/socket/server_socket__init.class or
server/socket/server_socket.clj on class path: 
In command line REPL, (use [server.socket server-socket]) gives me the same error message.
What might be wrong? I use emacs/cider/nREPL. This is the project.clj.
(defproject mire "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:dependencies [[org.clojure/clojure "1.5.1"]
[server-socket "1.0.0"]])
This is the code that causes an error:
(ns mire
(:use [clojure.contrib server-socket duck-streams]))
(def port (* 3 1111))
(defn mire-handle-client [in out]
(binding [*in* (reader in)
*out* (writer out)]
(loop []
(println (read-line))
(recur))))
(def server (create-server port mire-handle-client))
From the code in Building an Echo Server in Clojure - Part 2, this is the working code with server.socket.
(ns mire
(:use server.socket))
(import '[java.io BufferedReader InputStreamReader OutputStreamWriter])
(def port (* 3 1111))
(defn mire-handle-client [in out]
(binding [*in* (BufferedReader. (InputStreamReader. in))
*out* (OutputStreamWriter. out)]
(loop []
(println (read-line))
(recur))))
(def server (create-server port mire-handle-client))
The namespaces are usually dot-separated. server.socket in this library is.
Try this:
(ns mire (:use [server.socket]))

Execute compiled ClojureScript from the commandline with Rhino

I understand ClojureScript can be executed within a JavaScript REPL or it can be compiled into JavaScript, then run in a browser. I couldn't find a way to use it on the server side with Rhino. Here is my way of thinking, I have a simple source file:
(ns simple.hello)
(println "Hello, world")
I compile it to hello.js. I try to run
java -jar js.jar out/goog/base.js out/goog/deps.js out/hello.js
Nothing happens. How can I make it work, or is only Node.js supported on the command line?
This works for me:
src/app.js:
(ns simple.app)
(ns hello)
(defn ^:export greet [n]
(print (str "Hello " n)))
(greet "World")
To compile:
cljsc src '{:optimizations :advanced}' > app-prod.js
Then, to run with Rhino:
java -jar js.jar app-prod.js
Output: Hello World
Hope that helps!

Eval not working on unexpanded macro quote

In common lisp I can do this:
src-> (defmacro macro-hello ()
`"hello")
(eval '(macro-hello))
no problem.
In clojure:
(defmacro macro-hello []
`"hello")
(eval '(macro-hello))
gives me an error. Have I done something wrong?
Clojure Error:
Exception in thread "main" java.lang.Exception: Unable to resolve symbol: macro-hello in this context (NO_SOURCE_FILE:12)
at clojure.lang.Compiler.analyze(Compiler.java:4340)
at clojure.lang.Compiler.analyze(Compiler.java:4286)
at clojure.lang.Compiler$InvokeExpr.parse(Compiler.java:2767)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:4498)
at clojure.lang.Compiler.analyze(Compiler.java:4325)
at clojure.lang.Compiler.analyze(Compiler.java:4286)
at clojure.lang.Compiler$BodyExpr$Parser.parse(Compiler.java:3862)
at clojure.lang.Compiler$FnMethod.parse(Compiler.java:3697)
at clojure.lang.Compiler$FnMethod.access$1100(Compiler.java:3574)
at clojure.lang.Compiler$FnExpr.parse(Compiler.java:2963)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:4494)
at clojure.lang.Compiler.analyze(Compiler.java:4325)
at clojure.lang.Compiler.eval(Compiler.java:4530)
at clojure.core$eval__3990.invoke(core.clj:1728)
at com.yourcompany.defpackage$_main__4.invoke(defpackage.clj:12)
at clojure.lang.AFn.applyToHelper(AFn.java:171)
at clojure.lang.AFn.applyTo(AFn.java:164)
at com.yourcompany.defpackage.main(Unknown Source)
Caused by: java.lang.Exception: Unable to resolve symbol: macro-hello in this context
at clojure.lang.Compiler.resolveIn(Compiler.java:4682)
at clojure.lang.Compiler.resolve(Compiler.java:4628)
at clojure.lang.Compiler.analyzeSymbol(Compiler.java:4605)
at clojure.lang.Compiler.analyze(Compiler.java:4307)
... 17 more
Java Result: 1
[Edited]: added ending double quote
Your code works for me.
user> (defmacro macro-hello [] `"hello")
#'user/macro-hello
user> (eval '(macro-hello))
"hello"
This is via bleeding-edge Clojure. "Unable to resolve symbol" means it can't find the macro called macro-hello in the current namespace. Are you running this from the REPL or in a source file? I typed your statements at a REPL literally.
Not sure if this is a cause of problems for you, but please note the difference between ` and ' in Clojure. ` does namespace resolution and ' doesn't.
user> `macro-hello
user/macro-hello
user> 'macro-hello
macro-hello
This is different from Common Lisp's behavior. Backtick-quoting a String like `"hello" doesn't make much sense, since Strings don't belong to namespaces, but it also doesn't hurt anything.
(I'm assuming you made a typo in your Clojure code, with the missing double-quote.)
I like to work out of /opt on Mac and Linux boxes. To get the Clojure source. (% is Unix prompt)
% cd /opt
% git clone git://github.com/richhickey/clojure.git; #From Unix command line, you'll have an /opt/clojure dir
% cd /opt/clojure
% /opt/netbeans-6.7.1/java2/ant/bin/ant; # Run ant. It ships with Netbeans.
% cd /opt; # mkdir /opt if it's not there.
% git clone git://github.com/richhickey/clojure-contrib.git; # Get contrib
% /opt/netbeans-6.7.1/java2/ant/bin/ant -Dclojure.jar=../clojure/clojure.jar ; # Tell ant where clojure.jar is located
I rename jars to clojure.jar and clojure-contrib.jar
Copy these jars to your Netbean's project lib directory.