简体   繁体   English

如何使Clojure可见Java库?

[英]How do I make java libraries visible to clojure?

Ultimately, I want the following to work 最终,我希望以下工作

(ns grizzler.core
  (:import (com.sun.grizzly.http.embed GrizzlyWebServer)
           (com.sun.grizzly.tcp.http11 GrizzlyAdapter)))

However, I have no idea how to go about this. 但是,我不知道该如何处理。 Do I add stuff to my classpath? 我可以在我的类路径中添加内容吗? Where do I modify my classpath, in my .bashrc or within clojure? 我在哪里可以在.bashrc或clojure中修改类路径?

I've found the grizzly project at http://grizzly.java.net/ . 我在http://grizzly.java.net/找到了grizzly项目。 But what do I download? 但是我要下载什么呢? How do I install things? 我该如何安装东西? I really just have no idea what to do. 我真的不知道该怎么办。

Related: Using 3rd party java libraries, like com.jcraft.jsch, with clojure - except that it's not detailed enough for me :( 相关: 将第三方Java库(例如com.jcraft.jsch)与clojure一起使用 -只是它对我来说不够详细:(

Edit: I also tried the following in project.clj and it didn't work: 编辑:我也在project.clj中尝试了以下操作,但没有成功:

(defproject grizzler "1.0.0-SNAPSHOT"
  :description "FIXME: write description"
  :dependencies [[org.clojure/clojure "1.3.0"]
                 [com.sun.grizzly.http.core "2.1.10"]
                 [com.sun.grizzly.http.embed "2.1.10"]
                 [com.sun.grizzly.tcp.http11 "2.1.10"]])

In addition, I tried net.java.grizzly.http.core , and that didn't work either. 另外,我尝试了net.java.grizzly.http.core,但这也不起作用。

Thanks! 谢谢!

The current consensus on the "right" way to do this is basically "use leiningen". 当前关于“正确”方法的共识基本上是“使用莱宁根”。 Leiningen is basically a wrapper around maven and uses maven repositories. 莱宁根基本上是Maven的包装器,并使用Maven存储库。 Most of the time you can find the code you need in a maven repository somewhere, though eventually you will be in this situation where you need to use a jar file. 大多数时候,您可以在Maven存储库中的某个位置找到所需的代码,尽管最终您将需要使用jar文件。 In this case you install the jar file into your local maven repository which lives in your home directory and contains copies of all the jars required to build all your projects. 在这种情况下,您将jar文件安装到本地maven存储库中 ,该文件位于主目录中,并包含构建所有项目所需的所有jar的副本。 In cases where the jars you depend on are available from a central repo this local repo acts as a cache so you don't have to download them every time, though you can manually put jars into your local repo if it can't automatically download them. 如果可以从中央存储库中获得您依赖的jar,则此本地存储库将充当缓存,因此您不必每次都下载它们,尽管如果无法自动下载,则可以将jar手动放入本地存储库中他们。

I recommend watching this video first. 我建议先观看此视频

  • check carefully to see if the library you need is already available 仔细检查以查看所需的库是否已可用
  • download the jar you want 下载您想要的罐子
  • download leiningen 下载leiningen
  • run lein new nameOfYourProject 运行lein new nameOfYourProject
  • add it to your leiningen project's project.clj 将其添加到您的leiningen项目的project.clj
  • run lein deps lein deps
    • this will print the full command for installing the jar file to your local maven repo 这将打印用于将jar文件安装到本地Maven存储库的完整命令
    • run this command (as printed by leiningen) and your jar file should be copied to the correct location under ~/.m2/... 运行此命令(由leiningen打印),您的jar文件应复制到〜/ .m2 /下的正确位置。
    • run lein deps again to make sure it finds it 再次运行lein deps以确保找到它
  • add an import statement in your .clj file (as you have above) 在您的.clj文件中添加import语句(如上所述)
  • success! 成功!

Debugging this process can be very situation specific, but the denizens of #clojure are usually quite helpful. 调试此过程可能会因情况而异,但是#clojure的居民通常会很有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM