clojureinterfacejcs

Clojure access Java Interface constants


I am trying to access the constants from this file in Clojure:

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 attempt:

(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

How do I access these two values?


Solution

  • The following works with a local java interface:

    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 code:

    (ns tst.clj.core
      (:require  ...)
      (:import [jroot Const])
    )
    
    (println Const/ANSWER)
    
    ---------
    Const/ANSWER => 42
    

    Note the space in the :import vector (very important).

    Also, if the jar file is local use this syntax:

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