简体   繁体   English

Java与Clojure互操作:如何调用类?

[英]Clojure interop with Java: how to call a class?

I have a Java app that has a class at this address, inside a standard Maven layout: 我有一个Java应用程序,该应用程序在标准Maven布局内的该地址处有一个类:

src/main/java/com/ollio/nlp/Transformer.java 

The class and method that I want looks like this: 我想要的类和方法如下所示:

package com.ollio.nlp;

public class Transformer {
    public String transform(String JSONInput) {

I store the jar artifact locally in my Clojure app at this address: 我将罐子工件本地存储在我的Clojure应用程序中的以下地址:

maven_repository/local/nlp/1.0-SNAPSHOT/nlp-1.0-SNAPSHOT.jar 

I have tried a dozen variations to import it into my Clojure app, such as: 我尝试了十几种将其导入我的Clojure应用程序的方法,例如:

 (:import
    [com.ollio.nlp.Transformer])

But I keep getting the error "No such namespace". 但是我一直收到错误消息“没有这样的命名空间”。

What is the correct way to import this? 导入此内容的正确方法是什么?

EDITED: 编辑:

Here is how I currently try to do the import statement: 这是我目前尝试执行导入语句的方式:

(ns slick.query
  (:import
    [nlp.*])

I also tried: 我也尝试过:

(ns slick.query
  (:import
    [com.ollio.nlp.*])

I tried a few other variations. 我尝试了其他一些变体。

The project.clj file looks like this: project.clj文件如下所示:

(defproject slick "0.1"
  :description "slick is  an API for other ollio services, such as our mobile app."
  :dependencies [[org.clojure/clojure "1.6.0"]
             [com.taoensso/timbre "3.2.1"]
             [dire "0.5.1"]
             [slingshot "0.10.3"]
             [ring "1.4.0-RC1"]
             [clj-time "0.6.0"]
             [org.clojure/data.json "0.2.5"]
             [compojure "1.3.4"]
             [com.novemberain/monger "2.0.1"]
             [org.clojure/tools.namespace "0.2.4"]
             [manifold "0.1.0"]
             [me.raynes/fs "1.4.4"]
             [org.clojure/core.incubator "0.1.3"]
             [clj-stacktrace "0.2.7"]
             [overtone/at-at "1.2.0"]
             [ring/ring-json "0.3.1"]
             [clj-http "1.1.2"]
             [org.clojure/core.cache "0.6.4"]
             [cheshire "5.5.0"]
             [org.clojure/core.match "0.3.0-alpha4"]
             [local/nlp "1.0-SNAPSHOT"]]
  :repositories {"local" ~(str (.toURI (java.io.File. "maven_repository")))}
  :disable-implicit-clean true
  :source-paths      ["src/clojure"]
  :java-source-paths ["src/java"]
  :main slick.core
  :aot :all
  :jvm-opts ["-Xms100m" "-Xmx1000m" "-XX:-UseCompressedOops"])

If you are mixing java and clojure source code in one project, you should first review the lein docs: https://github.com/technomancy/leiningen/blob/master/doc/MIXED_PROJECTS.md 如果在一个项目中混合使用Java和clojure源代码,则应首先查看lein文档: https : //github.com/technomancy/leiningen/blob/master/doc/MIXED_PROJECTS.md

Also, if you posted your project.clj and the layout of your java/clojure sources, it would be easier to spot what is missing. 另外,如果您发布了project.clj和java / clojure源的布局,则更容易发现缺少的内容。

You probably want to change the last period in your :import statement into a space: 您可能需要将:import语句的最后一个句点更改为空格:

(ns mynamespace
  (:import [com.ollio.nlp Transformer]))

(EDIT: You can't use wildcards here. Every class that in com.ollio.nlp must be listed explicitly, separated by spaces.) That will allow you to use Transformer unqualified: (编辑:您不能在此处使用通配符com.ollio.nlp每个类com.ollio.nlp必须明确列出,并用空格分隔。)这将使您可以使用不合格的Transformer

(.transform (Transformer. <add constructor args here>) my-json-input)

As @noisesmith said, the :import statement should be part of an ns declaration. 正如@noisesmith所说, :import语句应该是ns声明的一部分。

There's also a good chance that there are problems with the way that the project is set up. 该项目的建立方式也很有可能出现问题。 You've given no indication that that is likely, but it often happens when one is starting to use Java interop, I believe. 您没有迹象表明这是可能的,但是我相信,这种情况通常在人们开始使用Java互操作时发生。 (It happened to me, in any event.) So @AlanThompson's advice may be relevant. (无论如何,这发生在我身上。)因此@AlanThompson的建议可能是有意义的。

You an also simply remove the :import statement, and use the Java class name in fully qualified form, eg: 您也可以简单地删除:import语句,并以完全限定的形式使用Java类名称,例如:

(.transform (com.ollio.nlp.Transformer. <add constructor args here>) my-json-input)

If you get an error when you do that, then there's probably a problem with the setup (unless you're using the class incorrectly). 如果这样做时出现错误,则可能是安装问题(除非您使用了不正确的类)。

(I'm not sure how helpful any of this. Alan Thompson's answer is probably the appropriate one.) (我不确定这有多有用。艾伦·汤普森的答案可能是合适的答案。)

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

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