Problem with Calling Custom Clojure Functions from Java

Problem with Calling Custom Clojure Functions from Java
0

#1

Hello, while I’m on my way to learn clojure API programming, I’ve encountered a rather tricky issue, Could anyone please give me a helping hand?

My issue is a related to the below link:
http://javajdk.net/tutorial/calling-custom-clojure-functions-from-java/

What disappoints me is the below line of code:
require.invoke(Clojure.read(“java-clj-interop.core”));

It does not work at all if I change the namespace to my own defined one. Below is the error message I got after I ran my java program (“yjj.java”) to call the clojure program (“core.clj”).

Exception in thread “main” java.io.FileNotFoundException: Could not locate lein2017/core__init.class or lein2017/core.clj on classpath.
at clojure.lang.RT.load(RT.java:456)
at clojure.lang.RT.load(RT.java:419)
at clojure.core$load$fn__5677.invoke(core.clj:5893)
at clojure.core$load.invokeStatic(core.clj:5892)
at clojure.core$load.doInvoke(core.clj:5876)
at clojure.lang.RestFn.invoke(RestFn.java:408)
at clojure.core$load_one.invokeStatic(core.clj:5697)
at clojure.core$load_one.invoke(core.clj:5692)
at clojure.core$load_lib$fn__5626.invoke(core.clj:5737)
at clojure.core$load_lib.invokeStatic(core.clj:5736)
at clojure.core$load_lib.doInvoke(core.clj:5717)
at clojure.lang.RestFn.applyTo(RestFn.java:142)
at clojure.core$apply.invokeStatic(core.clj:648)
at clojure.core$load_libs.invokeStatic(core.clj:5774)
at clojure.core$load_libs.doInvoke(core.clj:5758)
at clojure.lang.RestFn.applyTo(RestFn.java:137)
at clojure.core$apply.invokeStatic(core.clj:648)
at clojure.core$require.invokeStatic(core.clj:5796)
at clojure.core$require.doInvoke(core.clj:5796)
at clojure.lang.RestFn.invoke(RestFn.java:408)
at clojure.lang.Var.invoke(Var.java:379)
at lein2017.yjj.main(yjj.java:14)

To verify that my namespace “lein2017.core” is a valid namespace, I’ve also written a simple clojure program (“zbb.clj”) to invoke “core,clj”. Actually, it is working as normal, so namespace “lein2017.core” is a correct namespace.

And below is my programs:

(1) core.clj
;;;;;;;;;;;;;;;;;;;;;;; Code starts ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(ns lein2017.core)

(defn hello-world
"Yeah. You guessed it. This prints ‘Hello World!’"
[]
(println “Hello World!”))
;;;;;;;;;;;;;;;;;;;;;;; Code ends ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(2) yjj.java

package lein2017;
import clojure.java.api.Clojure;
import clojure.lang.IFn;

/**

  • Created by Administrator on 2017/7/16.
    */
    public class yjj {
    public static void main(String[] args) {

     // We use Clojure's require function to load the
     // Clojure file containing our hello-world function
     IFn require = Clojure.var("clojure.core", "require");
     require.invoke(Clojure.read("lein2017.core"));
    
     // Then we call our hello-world function from Java
     IFn helloWorld = Clojure.var(
             "lein2017.core",
             "hello-world");
    
     helloWorld.invoke();
    

    }
    }

(3) zbb.clj
;;;;;;;;;;;;;;;;;;;;;;; Code starts ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(ns lein2017.zbb
(:require [lein2017.core :as core]))

(core/hello-world)
;;;;;;;;;;;;;;;;;;;;;;; Code starts ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


#2

It seems nobody is interested in my issue? I’m really eager to know the answer. Could anyone give me some help? Thanks.


#3

I’ve been haunted by this issue for more than 3 days. The learning curve for me to learn clojure is too long.