Is it possible to determine that I did a 'lein repl'

101 views
Skip to first unread message

Cecil Westerhof

unread,
Feb 27, 2015, 3:09:39 AM2/27/15
to clo...@googlegroups.com
In my application I have a quit button which does:
    (System/exit 0)

But when I used 'lein repl' I do not want to exit, but just close the frame. Can this be done?

--
Cecil Westerhof

henry w

unread,
Feb 27, 2015, 4:01:16 AM2/27/15
to clo...@googlegroups.com
I had exactly that problem when using an old version of JDAF. The newer version makes it configurable.

to get around the problem i used AspectJ like this:

@Aspect
public class SystemExitEvader {

    @Pointcut("call(* java.lang.System.exit(..)) && args(status)")
    public void systemExitCall(int status){}

    @Around("systemExitCall(status)")
    public void doNothing(ProceedingJoinPoint thisJoinPoint, int status){
        System.out.println("Call to System.exit() attempted");
        // note: there is no call to proceed()
    }


}

btw, if you have any java code still, take a look at DCEVM which makes the whole 'develop java gui from repl' experience much better.

Cecil Westerhof

unread,
Feb 27, 2015, 4:16:04 AM2/27/15
to clo...@googlegroups.com
2015-02-27 10:01 GMT+01:00 henry w <henr...@gmail.com>:
I had exactly that problem when using an old version of JDAF. The newer version makes it configurable.

to get around the problem i used AspectJ like this:

@Aspect
public class SystemExitEvader {

    @Pointcut("call(* java.lang.System.exit(..)) && args(status)")
    public void systemExitCall(int status){}

    @Around("systemExitCall(status)")
    public void doNothing(ProceedingJoinPoint thisJoinPoint, int status){
        System.out.println("Call to System.exit() attempted");
        // note: there is no call to proceed()
    }


}

​How would I implement this in Clojure?

--
​​

Cecil Westerhof

henry w

unread,
Feb 27, 2015, 5:42:43 AM2/27/15
to clo...@googlegroups.com
Well, I dont know if you could. but unless anyone else chimes in with another solution... there's no problem using clojure and java together in a lein project.

And just to check ... this system/exit call is library code you have no control over right? I mean, if not of course you can stick a when block around it looking for a system property set from the repl or something along those lines.

Cecil Westerhof

unread,
Feb 27, 2015, 7:14:01 AM2/27/15
to clo...@googlegroups.com
2015-02-27 11:42 GMT+01:00 henry w <henr...@gmail.com>:
Well, I dont know if you could. but unless anyone else chimes in with another solution... there's no problem using clojure and java together in a lein project.

And just to check ... this system/exit call is library code you have no control over right? I mean, if not of course you can stick a when block around it looking for a system property set from the repl or something along those lines.


​The system/exit call is in my own code. When closing the main frame, the application should normally terminate, but not when I called it from the REPL, then I should just close the window. So that if I could see it was called from the REPL then I could only close and not exit. I could set a variable before I call -main, but I would prefer it when I did not need to. ;-)
​​

--
Cecil Westerhof

Sean Corfield

unread,
Feb 27, 2015, 8:29:56 AM2/27/15
to clo...@googlegroups.com
On Feb 27, 2015, at 12:09 AM, Cecil Westerhof <cldwes...@gmail.com> wrote:
In my application I have a quit button which does:
    (System/exit 0)

But when I used 'lein repl' I do not want to exit, but just close the frame. Can this be done?

Here’s how we do it. Add the following to project.clj:

  :profiles {:repl {:jvm-opts ~(conj (jvm-opts) "-Dlein.profile.repl=true")}}

jvm-opts is a function in project.clj that returns the default JVM options — we do different things on different platforms — it returns a vector of JVM options. So :jvm-opts ["-Dlein.profile.repl=true"] might be sufficient for you.

Then in our code we test:

(System/getProperty "lein.profile.repl")

This will be true if you’re in a REPL and nil otherwise.

Sean Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/

"Perfection is the enemy of the good."
-- Gustave Flaubert, French realist novelist (1821-1880)



Cecil Westerhof

unread,
Feb 27, 2015, 9:56:39 AM2/27/15
to clo...@googlegroups.com
2015-02-27 14:29 GMT+01:00 Sean Corfield <se...@corfield.org>:
On Feb 27, 2015, at 12:09 AM, Cecil Westerhof <cldwes...@gmail.com> wrote:
In my application I have a quit button which does:
    (System/exit 0)

But when I used 'lein repl' I do not want to exit, but just close the frame. Can this be done?

Here’s how we do it. Add the following to project.clj:

  :profiles {:repl {:jvm-opts ~(conj (jvm-opts) "-Dlein.profile.repl=true")}}


​That gives:
java.lang.Exception: Error loading /home/cecil/Clojure/quotes/project.clj
        at leiningen.core.project$read$fn__3326.invoke(project.clj:696)
        at leiningen.core.project$read.invoke(project.clj:693)
        at leiningen.core.project$read.invoke(project.clj:703)
        at leiningen.core.main$_main$fn__3092.invoke(main.clj:294)
        at leiningen.core.main$_main.doInvoke(main.clj:290)
        at clojure.lang.RestFn.invoke(RestFn.java:408)
        at clojure.lang.Var.invoke(Var.java:415)
        at clojure.lang.AFn.applyToHelper(AFn.java:161)
        at clojure.lang.Var.applyTo(Var.java:532)
        at clojure.core$apply.invoke(core.clj:617)
        at clojure.main$main_opt.invoke(main.clj:335)
        at clojure.main$main.doInvoke(main.clj:440)
        at clojure.lang.RestFn.invoke(RestFn.java:436)
        at clojure.lang.Var.invoke(Var.java:423)
        at clojure.lang.AFn.applyToHelper(AFn.java:167)
        at clojure.lang.Var.applyTo(Var.java:532)
        at clojure.main.main(main.java:37)
Caused by: java.lang.RuntimeException: Unable to resolve symbol: jvm-opts in this context, compiling:(/home/cecil/Clojure/quotes/project.clj:9:38)

--
Cecil Westerhof

Cecil Westerhof

unread,
Feb 27, 2015, 10:11:17 AM2/27/15
to clo...@googlegroups.com

​Bit this works:
    :profiles {:repl {:jvm-opts ["-Dlein.profile.repl=true"]}}

--
Cecil Westerhof

Sean Corfield

unread,
Feb 27, 2015, 3:10:43 PM2/27/15
to clo...@googlegroups.com
On Feb 27, 2015, at 6:56 AM, Cecil Westerhof <cldwes...@gmail.com> wrote:
Caused by: java.lang.RuntimeException: Unable to resolve symbol: jvm-opts in this context, compiling:(/home/cecil/Clojure/quotes/project.clj:9:38)

Right, as I said:

"jvm-opts is a function in project.clj that returns the default JVM options — we do different things on different platforms — it returns a vector of JVM options. So :jvm-opts ["-Dlein.profile.repl=true"] might be sufficient for you."

Sean Corfield -- (904) 302-SEAN

Cecil Westerhof

unread,
Feb 27, 2015, 4:21:11 PM2/27/15
to clo...@googlegroups.com
2015-02-27 21:10 GMT+01:00 Sean Corfield <se...@corfield.org>:
On Feb 27, 2015, at 6:56 AM, Cecil Westerhof <cldwes...@gmail.com> wrote:
Caused by: java.lang.RuntimeException: Unable to resolve symbol: jvm-opts in this context, compiling:(/home/cecil/Clojure/quotes/project.clj:9:38)

Right, as I said:

​You are right: I did not read good enough. :-(​
 

 
"jvm-opts is a function in project.clj that returns the default JVM options — we do different things on different platforms — it returns a vector of JVM options. So :jvm-opts ["-Dlein.profile.repl=true"] might be sufficient for you."
​​

--
Cecil Westerhof
Reply all
Reply to author
Forward
0 new messages