Re: Troubles working with nl.bitwalker.useragentutils

610 views
Skip to first unread message

Herwig Hochleitner

unread,
Oct 24, 2012, 8:58:46 AM10/24/12
to clo...@googlegroups.com
2012/10/24 Andrii V. Mishkovskyi <mish...@gmail.com>
lein deps runs just fine, but trying to import the package in the namespace fails with the following exception: "java.lang.ClassNotFoundException: nl.bitwalker.useragentutils.UserAgent". Here's the complete helloworld/core.clj listing:

(ns helloworld.core
  (:import [nl.bitwalker.useragentutils UserAgent])
  (:gen-class :main true))


Not sure why that doesn't work. The idimatic form, however, is either:

(ns ... (:import pgk.ClassName))

or the prefix list form with round parens

(ns ... (:import (pkg ClassNameA ClassNameB ...)))
 
Now, changing the import to look like (:import [nl.bitwalker.useragentutils.UserAgent]) actually makes the compile succeed, but then I have no idea how to access UserAgent class, since neither (UserAgent. ) nor (nl.bitwalker.useragentutils/UserAgent. ) works when trying to compile the project.


The constructor takes a String argument. There is also a static factory, which is IMO preferrable: http://user-agent-utils.java.net/javadoc/nl/bitwalker/useragentutils/UserAgent.html#parseUserAgentString(java.lang.String)

call like (UserAgent/parseUserAgentString "uas") 

AtKaaZ

unread,
Oct 24, 2012, 9:41:26 AM10/24/12
to clo...@googlegroups.com
wow I can't believe that worked, I mean look:
(ns helloworld.core
  (:import nl.bitwalker.useragentutils.UserAgent)
  (:gen-class :main true))

(defn -main
  [& args]
  (println (UserAgent/parseUserAgentString "uas"))
)
 
this works, but replace the import line with either this:
(:import [nl.bitwalker.useragentutils.UserAgent])
or this:
(:import (nl.bitwalker.useragentutils.UserAgent))
and you get this error:

c:\1\helloworld>lein compile
Compiling helloworld.core
Exception in thread "main" java.lang.RuntimeException: No such namespace: UserAgent, compiling:(helloworld/core.clj:7)
        at clojure.lang.Compiler.analyze(Compiler.java:6281)
        at clojure.lang.Compiler.analyze(Compiler.java:6223)
        at clojure.lang.Compiler$InvokeExpr.parse(Compiler.java:3497)
        at clojure.lang.Compiler.analyzeSeq(Compiler.java:6457)
        at clojure.lang.Compiler.analyze(Compiler.java:6262)
        at clojure.lang.Compiler.analyze(Compiler.java:6223)
        at clojure.lang.Compiler$InvokeExpr.parse(Compiler.java:3548)
        at clojure.lang.Compiler.analyzeSeq(Compiler.java:6457)
        at clojure.lang.Compiler.analyze(Compiler.java:6262)
        at clojure.lang.Compiler.analyze(Compiler.java:6223)
        at clojure.lang.Compiler$BodyExpr$Parser.parse(Compiler.java:5618)
        at clojure.lang.Compiler$FnMethod.parse(Compiler.java:5054)
        at clojure.lang.Compiler$FnExpr.parse(Compiler.java:3674)
        at clojure.lang.Compiler.analyzeSeq(Compiler.java:6453)
        at clojure.lang.Compiler.analyze(Compiler.java:6262)
        at clojure.lang.Compiler.analyzeSeq(Compiler.java:6443)
        at clojure.lang.Compiler.analyze(Compiler.java:6262)
        at clojure.lang.Compiler.access$100(Compiler.java:37)
        at clojure.lang.Compiler$DefExpr$Parser.parse(Compiler.java:518)
        at clojure.lang.Compiler.analyzeSeq(Compiler.java:6455)
        at clojure.lang.Compiler.analyze(Compiler.java:6262)
        at clojure.lang.Compiler.analyze(Compiler.java:6223)
        at clojure.lang.Compiler.compile1(Compiler.java:7030)
        at clojure.lang.Compiler.compile(Compiler.java:7097)
        at clojure.lang.RT.compile(RT.java:387)
        at clojure.lang.RT.load(RT.java:427)
        at clojure.lang.RT.load(RT.java:400)
        at clojure.core$load$fn__4890.invoke(core.clj:5415)
        at clojure.core$load.doInvoke(core.clj:5414)
        at clojure.lang.RestFn.invoke(RestFn.java:408)
        at clojure.core$load_one.invoke(core.clj:5227)
        at clojure.core$compile$fn__4895.invoke(core.clj:5426)
        at clojure.core$compile.invoke(core.clj:5425)
        at user$eval31.invoke(NO_SOURCE_FILE:1)
        at clojure.lang.Compiler.eval(Compiler.java:6511)
        at clojure.lang.Compiler.eval(Compiler.java:6501)
        at clojure.lang.Compiler.eval(Compiler.java:6477)
        at clojure.core$eval.invoke(core.clj:2797)
        at clojure.main$eval_opt.invoke(main.clj:297)
        at clojure.main$initialize.invoke(main.clj:316)
        at clojure.main$null_opt.invoke(main.clj:349)
        at clojure.main$main.doInvoke(main.clj:427)
        at clojure.lang.RestFn.invoke(RestFn.java:421)
        at clojure.lang.Var.invoke(Var.java:419)
        at clojure.lang.AFn.applyToHelper(AFn.java:163)
        at clojure.lang.Var.applyTo(Var.java:532)
        at clojure.main.main(main.java:37)
Caused by: java.lang.RuntimeException: No such namespace: UserAgent
        at clojure.lang.Util.runtimeException(Util.java:170)
        at clojure.lang.Compiler.resolveIn(Compiler.java:6736)
        at clojure.lang.Compiler.resolve(Compiler.java:6710)
        at clojure.lang.Compiler.analyzeSymbol(Compiler.java:6671)
        at clojure.lang.Compiler.analyze(Compiler.java:6244)
        ... 46 more
Compilation failed.

The error doesn't help you solve the problem... which is: don't pass a seq to :import

Thank you HH.

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en



--
I may be wrong or incomplete.
Please express any corrections / additions,
they are encouraged and appreciated.
At least one entity is bound to be transformed if you do ;)

Jim foo.bar

unread,
Oct 24, 2012, 9:48:08 AM10/24/12
to clo...@googlegroups.com
On 24/10/12 14:41, AtKaaZ wrote:
The error doesn't help you solve the problem... which is: don't pass a seq to :import

You can easily pass seqs to :import...this works fine for me (importing 3 classes from the same package):

(:import [encog_java.customGA CustomNeuralGeneticAlgorithm                           CustomGeneticScoreAdapter Referee])

you just need a space after the package-name...

try 


(:import [nl.bitwalker.useragentutils UserAgent])

hope that helps

Jim

Jim foo.bar

unread,
Oct 24, 2012, 9:49:58 AM10/24/12
to clo...@googlegroups.com
aaa sorry I can see from a previous post that you already tried that! this is strange!

Jim

Ben Mabey

unread,
Oct 24, 2012, 12:03:02 PM10/24/12
to clo...@googlegroups.com


I'm completely lost here. Any tips? Maybe somebody has experience working with other UA parsing libraries?


Hi Andrii,
I have used the same library you are attempting to use in the past and it has worked well.  My needs were simple and I found the bitwalker lib to be a great library for those needs.  Here is working code showing how I used it (note, you can get more data from it but I was only interested in a subset):

(ns rbl.feature-extraction.user-agent
  (:use [clojure.core.memoize :only [memo]])
  (:require [clojure.tools.logging :as log])
  (:import [nl.bitwalker.useragentutils UserAgent DeviceType Browser OperatingSystem]))

(defn str->features [string]
  (try
    (let [user-agent (UserAgent. (or string ""))]
      {:browser_group (-> user-agent .getBrowser .getGroup .getName)
       :os_group (-> user-agent .getOperatingSystem .getGroup .getName)
       :device_type (-> user-agent .getOperatingSystem .getDeviceType .getName)})
    (catch Exception e
      (log/error (str "Could not derive the user-agent from " string) e)
      (str->features nil))))

(def possible-features
  (memo (fn []
          {:os_group (set (map #(-> % .getGroup .getName) (OperatingSystem/values)))
           :browser_group (set (map #(-> % .getGroup .getName) (Browser/values)))
           :device_type (set (map #(.getName %) (DeviceType/values)))})))


In gist form along with some midje facts: https://gist.github.com/3946906


Hope that helps,

Ben



AtKaaZ

unread,
Oct 24, 2012, 12:02:59 PM10/24/12
to clo...@googlegroups.com
no no I didn't try that, you are right, it works with space and I understand why now(avoid repeating the pkg)

  (:import [nl.bitwalker.useragentutils UserAgent])

I guess what I was trying to say is that, it's too easy to make the mistake and the error wouldn't help you.

Thanks.



--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
Reply all
Reply to author
Forward
0 new messages