动态地添加到clojure repl中的classpath以访问Java类文件(石榴/ add-classpath)

时间:2018-09-28 01:04:01

标签: java clojure classpath read-eval-print-loop

我已经在shortScreen/out/production/classes/com/rsw/中编译了类文件,并希望在clojure repl中使用它们。我认为挂断是在add-classpath上,但是我不知道会发生什么。或如何解决。

(use '[cemerick.pomegranate :only (add-dependencies add-classpath)])
(import 'clojure.pprint)
(def myclasses "shortScreen/out/production/classes/com/rsw/")
; do i have the correct path?
(-> (clojure.java.io/file myclasses) (file-seq) (pprint)) ; shows many class files
(.exists (clojure.java.io/file myclasses "util.class")) ; true

; add the path
(add-classpath myclasses)

; but nothing like myclasses path in list
(def thiscp (.getURLs (java.lang.ClassLoader/getSystemClassLoader)))
(pprint thiscp)
(filter (fn [s] (re-seq #"out" s)) ;myclasses path should match "out"
     (for [x thiscp]
       (.toString x)))

; fails -- but not sure if this is the correct syntax anyway
(import 'com.rsw.util)
(util/msg "test")

编辑:可复制的示例

mkdir example && cd $_
# java code
cat > util.java << EOF
 package com.util;
 public class util {
 public static void msg(String a){
   System.out.println(a);
  }
 }
EOF

# clojure depends
cat > deps.edn << EOF
  {:deps {
    :alembic {:mvn/version "0.3.2"}
    :com.cemerick/pomegranate {:mvn/version "1.0.0"}
    }
  }
EOF

# compile java to class
javac util.java
ls # util.java util.class

# try to load in repl
clj

  (use '[cemerick.pomegranate :only (add-dependencies add-classpath)])
  (add-classpath (-> (java.io.File. ".") .getAbsolutePath))
  (import 'com.util.util)
  ; ClassNotFoundException com.util  java.net.URLClassLoader.findClass (URLClassLoader.java:381)

0 个答案:

没有答案