Hello clojurians,

It seems not to be able to control the whole classpath of my runtime.
But my aim, compiling with a path given at runtime and execute it, has been 
achieved,
with
  (.setContextClassLoader (Thread/currentThread)
    (DynamicClassLoader. (.getContextClassLoader (Thread/currentThread)))

Details reported below.

Regards,
Yoshinori Kohyama



$ cat project.clj 
(defproject dce "0.0.1"
  :description "Dynamic Compling And Execution"
  :dependencies [[org.clojure/clojure "1.3.0"]]
  :main dce.core)

$ cat src/dce/core.clj 
(ns dce.core
  (:import java.net.URL clojure.lang.DynamicClassLoader)
  (:gen-class))

(defn -main [abs-path target-ns & args]
  (let [ccl (.getContextClassLoader (Thread/currentThread))
        dcl (if (instance? DynamicClassLoader ccl) ccl
                (let [l (DynamicClassLoader. ccl)]
                  (.setContextClassLoader (Thread/currentThread) l)
                  l))]
    (.addURL dcl (URL. (str "file://" abs-path "/src/")))
    (.addURL dcl (URL. (str "file://" abs-path "/classes/")))
    (binding [*compile-path* (str abs-path "/classes")]
      (compile (symbol target-ns)))
    (def f (future (apply (resolve (symbol (str target-ns "/-main"))) 
args)))
    (Thread/sleep 5000)
    (future-cancel f)))

$ tree samples
samples
├── classes
└── src
    └── foo
        └── core.clj

3 directories, 1 file

$ lein repl
REPL started; server listening on localhost port 5997
dce.core=> (-main (.getCanonicalPath (java.io.File. "samples")) "foo.core" 
"arg1" "arg2")
Foo:  arg1 arg2
Foo:  arg1 arg2
Foo:  arg1 arg2
Foo:  arg1 arg2
Foo:  arg1 arg2
Foo:  arg1 arg2
true
dce.core=> ^D

$ tree samplessamples
├── classes
│   └── foo
│       ├── core$_main.class
│       ├── core$loading__4505__auto__.class
│       └── core__init.class
└── src
    └── foo
        └── core.clj

4 directories, 4 files

$ lein uberjar
...
Created ... dce/dce-0.0.1-standalone.jar

$ rm -Rf samples/classes/*
$ tree samples
samples
├── classes
└── src
    └── foo
        └── core.clj

3 directories, 1 file

$ java -jar dce-0.0.1-standalone.jar `pwd`/samples foo.core arg1 arg2
Foo:  arg1 arg2
Foo:  arg1 arg2
Foo:  arg1 arg2
Foo:  arg1 arg2
Foo:  arg1 arg2
^C

$ tree samples
samples
├── classes
│   └── foo
│       ├── core$_main.class
│       ├── core$loading__4505__auto__.class
│       └── core__init.class
└── src
    └── foo
        └── core.clj

4 directories, 4 files

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to