Clojure访问Java接口常量

时间:2017-06-02 16:08:12

标签: clojure interface jcs

我正在尝试从Clojure中的this file访问常量:

public interface CacheConstants
{
    /** This is the name of the config file that we will look for by default. */
    String DEFAULT_CONFIG = "/cache.ccf";

    /** Delimiter of a cache name component. This is used for hierarchical deletion */
    String NAME_COMPONENT_DELIMITER = ":";
 }

Clojure的尝试:

(import '[org.apache.jcs.engine.CacheConstants])
org.apache.jcs.engine.CacheConstants/DEFAULT_CONFIG

;; clojure.lang.Compiler$CompilerException: java.lang.ClassNotFoundException: org.apache.jcs.engine.CacheConstants

如何访问这两个值?

1 个答案:

答案 0 :(得分:3)

以下适用于本地java接口:

file:src-java / jroot / Const.java

package jroot;
public interface Const {
  long ANSWER = 42;

file:project.clj

(defproject clj "0.1.0-SNAPSHOT"
  <snip>
  :java-source-paths ["src-java"]
)

Clojure代码:

(ns tst.clj.core
  (:require  ...)
  (:import [jroot Const])
)

(println Const/ANSWER)

---------
Const/ANSWER => 42

注意:import向量中的空格(非常重要)。

此外,如果the jar file is local使用以下语法:

(defproject test-project "0.1.0-SNAPSHOT"
:description "Blah blah blah"
...
:resource-paths ["resources/Siebel.jar" "resources/SiebelJI_enu.jar"])