:require data.json in Clojure doesn't seem to be working - clojure

I am new to Clojure and have been stuck for a while on the :require. I am using lein and included in my project.clj
:dependencies [[org.clojure/clojure "1.6.0"]
[http-kit "2.1.18"]
[org.clojure/data.json "0.2.6"]])
I checked the class path and tried to :require the data.json as it said on the data.json Github page but it says cannot find on classpath. I tried
:require [org.httpkit.client :as http]
[clojure.string :as str]
[clojure.data.json :as json]))
along with org.clojure.data.json and a whole bunch of other things. In my classpath it says
.m2/repository/org/clojure/data.json/0.2.6/data.json-0.2.6.jar
So I know it is in my classpath.
Thanks
Edit
My full ns script in my core.clj is
(ns myproject.core
(import [java.net URLEncoder])
(:require [org.httpkit.client :as http]
[clojure.string :as str]
[clojure.data.json :as json]))
Just for clarification I am importing one project into another using the /checkouts/. When I delete the data.json line it works but doesn't work with it.

if you're using lein try running lein install - this should make the jars available to you.
I'm using the same library in a current project and my project.clj and ns :require are both identical to yours.

Related

Referencing macros in ClojureScript namespace

I was trying to reference macros in my ClojureScript namespace:
(ns swagger-service.core
(:require [reagent.core :as reagent :refer [atom]]
[ajax.core :refer [GET]])
(:require-macros [secretary.core :refer [defroute]]))
But I got the following error:
java.lang.RuntimeException
No such var: clojure.core/require-macros
You can find a working example here to clone: https://github.com/cloojure/cljs-template. You probably want a syntax something more like this:
(ns tst.flintstones.pebbles
(:require
[clojure.string :as str]
[flintstones.test-cljs :refer [dotest is isnt is= isnt= testing use-fixtures]] ))
As akond pointed out, it appears you are invoking the Clojure compiler, not the ClojureScript compiler.
Be sure to read and follow the README for the above repo as it is easy to make a mistake setting up the config for a CLJS project.

Could not locate clojure/data/xml__init.class in a luminus project

In my luminus project I've added this:
[org.clojure/data.zip "0.1.2"]
to the list of dependencies but this throws an exception still:
(ns myapp.rss
(:use [clojure.data.xml :as xml :only [emit]]))
which is:
Could not locate clojure/data/xml__init.class or clojure/data/xml.clj on classpath
here is a working example to compare with:
project.clj:
(defproject hello "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"}
:main hello.core
:dependencies [[org.clojure/clojure "1.7.0"]
[org.clojure/data.xml "0.0.8"]
[org.clojure/data.zip "0.1.2"]
[clj-http "2.2.0"]])
from core.clj:
(ns hello.core
(:require [clj-http.client :as http-client]
[clojure.zip :as zip]
[clojure.xml :as xml]
[clojure.data.xml :as xml-data :refer [emit]]
[clojure.data.zip.xml :as xml-z]))
(use ... :only) has been deprecated by the require :refer pattern.
And here are some common things to check:
you have actually fetched the dependencies since adding them to the project.clj file
Try running lein deps from the command line to make sure fetching the dependencies worked
restart cider (if in emacs)
try from lein repl
if none of this works look in ~/.m2/repository and make sure the class files are there
run ps -ef (if in linux) to look at the command used to start java and make sure the classpath contains your dependency.

Can't build a jar using Leiningen

I'm trying to make a stand alone jar from my bare-bones Clojure project using the Leiningen plugin in Intellij's Cursive.
To create the project, I just created the project.clj file, opened it, and Cursive offered to import it as a project.
project.clj:
(defproject WaterTimer "1"
:description "A timer that reminds you to drink water"
:main tone-producer/main)
tone-producer.clj:
(ns tone-producer
(:require [general-helpers :as g])
(:import [javax.sound.midi MidiSystem
Synthesizer
MidiChannel])
(:gen-class))
(defn main [& args]
(println "Test!"))
When I run the "uberjar" task, I get the following output:
Warning: specified :main without including it in :aot.
Implicit AOT of :main will be removed in Leiningen 3.0.0.
If you only need AOT for your uberjar, consider adding :aot :all into your
:uberjar profile instead.
Warning: The Main-Class specified does not exist within the jar. It may not be executable as expected. A gen-class directive may be missing in the namespace which contains the main method.
Created C:\Users\slomi\IdeaProjects\WaterTimer\target\WaterTimer-1.jar
Created C:\Users\slomi\IdeaProjects\WaterTimer\target\WaterTimer-1-standalone.jar
I also tried changing the main function to have the default name, and omit the name from the defproject:
(defproject WaterTimer "1"
:description "A timer that reminds you to drink water"
:main tone-producer)
(ns tone-producer
(:require [general-helpers :as g])
(:import [javax.sound.midi MidiSystem
Synthesizer
MidiChannel])
(:gen-class))
(defn -main [& args]
(println "Test!"))
But now I get the error:
Error: Could not find or load main class clojure.main
Compilation failed: Subprocess failed
The structure is:
WaterTimer
src
tone-producer.clj
project.clj
target
Any guidance here would be appreciated.
After a bit of fiddling
I dropped (:require [general-helpers :as g]) since its not necessary to demostrate the issue
Error: Could not find or load main class clojure.main Compilation failed
you didn't include the clojure dependency [1]
:gen-class needs AOT - as Sanchayan pointed out
see [2]
project.clj
(defproject WaterTimer "0.0.1"
:description "A timer that reminds you to drink water"
:dependencies [[org.clojure/clojure "1.8.0"]] ;; <- [1]
:main tone-producer
:aot [tone-producer]) ;; <- [2]
src/tone_producer.clj - USE '_' instead of '-' in the filename
(ns tone-producer
(:import [javax.sound.midi MidiSystem
Synthesizer
MidiChannel])
(:gen-class))
(defn -main [& args]
(println "Test!"))
Result:
$ lein uberjar
Compiling tone-producer
Compiling tone-producer
Created .../watertimer/target/WaterTimer-0.0.1.jar
Created .../watertimer/target/WaterTimer-0.0.1-standalone.jar
$ java -jar target/WaterTimer-0.0.1-standalone.jar
Test!
Generally I'd recommend to init a project with lein new <name> via command line and the import it into Cursive/Other IDE of choice.
For creating uberjars, the project file should have the :aot keyword enabling ahead of time compilation.
Here is an output from my project.clj file.
(defproject jdbc "0.1.0-SNAPSHOT"
:description "JDBC Project"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.8.0"]
[org.clojure/java.jdbc "0.6.1"]
[postgresql "9.3-1102.jdbc41"]
[com.mchange/c3p0 "0.9.5.2"]
[byte-streams "0.2.2"]]
:main jdbc.core
:aot [jdbc.core])
Note the :main and :aot entries. Also it needs to be -main as already stated by birdspider.

How do I fix this dependency issue in Clojure?

I'm having a lot of trouble fixing an issue where the dependencies for two different packages are colliding. My project.clj's dependencies look like this:
:dependencies [[org.clojure/clojure "1.6.0"]
[itsy "0.1.1"]
[amazonica "0.3.22" :exclusions [commons-logging org.apache.httpcomponents/httpclient com.fasterxml.jackson.core/jackson-core]]])
My namespace looks like this:
(ns crawler.core
(:require [itsy.core :refer :all])
(:require [itsy.extract :refer :all])
(:use [amazonica.core]
[amazonica.aws.s3]))
When I try to load the namespace into lein's repl with (load crawler/core), I get this error:
CompilerException java.lang.NoSuchMethodError: com.fasterxml.jackson.core.JsonFactory.requiresPropertyOrdering()Z, compiling:(amazonica/core.clj:1:1)
Online sources suggest that this is a dependency mismatch. How do I fix it?
I put the exclusion on itsy rather than amazonica and it worked. Also fixed the NS form in core.clj.
project.clj:
(defproject blabla "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.6.0"]
[itsy "0.1.1" :exclusions [com.fasterxml.jackson.core/jackson-core]]
[amazonica "0.3.22" :exclusions [commons-logging org.apache.httpcomponents/httpclient]]])
core.clj:
(ns blabla.core
(:require [itsy.core :refer :all]
[itsy.extract :refer :all]
[amazonica.core :refer :all]
[amazonica.aws.s3 :refer :all]))
(defn foo
"I don't do a whole lot."
[x]
(println x "Hello, World!"))
to deal with these situatuions in general run
lein deps :tree
and add exclusions until only the newest versions remain.

What is the correct terminology to describe what goes on in project.clj and core.clj?

When explaining Clojure builds, I would like to use the correct terminology. Therefore my overarching question is, am I using the correct terminology in the following examples?
Given one of my project.clj files:
(defproject bene-csv "1.0.4-SNAPSHOT"
:description "A csv parsing library"
:dependencies [[org.clojure/clojure "1.3.0"]
[clojure-csv/clojure-csv "1.3.2"]
[util "1.0.2-SNAPSHOT"]]
:aot [bene-csv.core]
:omit-source true)
I believe the correct terminology is I am creating dependencies to Clojure 1.3.0, clojure-csv, and one my my modules, which is named util.
Is that correct?
Given the header of my core.clj
(ns bene-csv.core
^{:author "Charles M. Norton",
:doc "bene-csv is a small library to parse a .csv file.
Created on March 8, 2012"}
(:require [clojure.string :as cstr])
(:require [util.core :as utl])
(:use clojure-csv.core))
am I including or referencing these modules, or should I be using different terminology?
Thank you.
In the project.clj you are defining the dependencies for specific packages (or projects) that will be required in classpath for you project to work.
In the core.clj you are referencing to the namespaces or importing the namespace vars depending on what you use (:use or :require)