public inbox for kawa@sourceware.org
 help / color / mirror / Atom feed
* java -Dplugins.dir=./jars kawa.repl
@ 2015-09-30 19:53 David Pirotte
  2015-09-30 20:25 ` Per Bothner
  0 siblings, 1 reply; 10+ messages in thread
From: David Pirotte @ 2015-09-30 19:53 UTC (permalink / raw)
  To: kawa

[-- Attachment #1: Type: text/plain, Size: 576 bytes --]

Hello,

I finally discovered a solution to my problem of scripting imagej using kawa, here
is it:

	export CLASSPATH=.:./jars/ij.jar:./jars/nl-mean.jar:/opt/share/java/kawa-2.0.1.jar
	java -Dplugins.dir=./jars kawa.repl
		(import (ij-core) (ij-nl-mean))
		(define iplus1 (ij-open "images/nlm-small.png"))
		(ij-nl-mean iplus1 3)

Good, but I would like to set this plugins.dir from kawa instead, so I can do:

	export CLASSPATH=.:./jars/ij.jar:./jars/nl-mean.jar
	kawa
		(set! plugins.dir "./jars")
		...

But that fails, how do I do that?

Thanks
David

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 473 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: java -Dplugins.dir=./jars kawa.repl
  2015-09-30 19:53 java -Dplugins.dir=./jars kawa.repl David Pirotte
@ 2015-09-30 20:25 ` Per Bothner
  2015-09-30 20:42   ` Jamison Hope
  2015-09-30 22:33   ` David Pirotte
  0 siblings, 2 replies; 10+ messages in thread
From: Per Bothner @ 2015-09-30 20:25 UTC (permalink / raw)
  To: David Pirotte, kawa



On 09/30/2015 12:52 PM, David Pirotte wrote:
> Hello,
>
> I finally discovered a solution to my problem of scripting imagej using kawa, here
> is it:
>
> 	export CLASSPATH=.:./jars/ij.jar:./jars/nl-mean.jar:/opt/share/java/kawa-2.0.1.jar
> 	java -Dplugins.dir=./jars kawa.repl
> 		(import (ij-core) (ij-nl-mean))
> 		(define iplus1 (ij-open "images/nlm-small.png"))
> 		(ij-nl-mean iplus1 3)
>
> Good, but I would like to set this plugins.dir from kawa instead, so I can do:
>
> 	export CLASSPATH=.:./jars/ij.jar:./jars/nl-mean.jar
> 	kawa
> 		(set! plugins.dir "./jars")
> 		...
>
> But that fails, how do I do that?

It would need some non-trivial programming.  (It could be done in Scheme,
but it requires understand the JVM classloading mechanism, among other things.)
It shouldn't be difficult to write a DynamicClassLoader class that searches
for a class in a list of jars, where the list depend on plugins.dir.

Basically, you'd be bypassing the system class loader.  We don't want to
do that, at least by default, because it by-passes whatever optimizations
and possible tricks the JVM uses, so it might slow down startup.

Another problem is class unloading: If you change plugins.dir, what
happens to old classes that were loaded using the previous value?
Existing objects may use the old class, as may existing bytecode.
How do you handle the inconsistencies?

This is the kind of feature that can be useful in interactive uses
of Kawa, as well as program development.  Kawa has a strong focus on
performance, including various kinds of inlining, and type propagation.
This means that interactive re-loading of code may cause consistencies
between code and objects that assume different versions.

I'm working on part of the puzzle for making Kawa more robust when
functions are re-loaded.  More dynamic/interactive use is a work-in-progress,
and has to be done so we don't hurt Kawa's performance advantage.

My advance: Don't try to change the plugins.dir after the JVM running Kawa has started up.
-- 
	--Per Bothner
per@bothner.com   http://per.bothner.com/

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: java -Dplugins.dir=./jars kawa.repl
  2015-09-30 20:25 ` Per Bothner
@ 2015-09-30 20:42   ` Jamison Hope
  2015-09-30 20:57     ` Per Bothner
  2015-09-30 22:33   ` David Pirotte
  1 sibling, 1 reply; 10+ messages in thread
From: Jamison Hope @ 2015-09-30 20:42 UTC (permalink / raw)
  To: kawa


On Sep 30, 2015, at 4:24 PM, Per Bothner <per@bothner.com> wrote:

> 
> 
> On 09/30/2015 12:52 PM, David Pirotte wrote:
>> Hello,
>> 
>> I finally discovered a solution to my problem of scripting imagej using kawa, here
>> is it:
>> 
>> 	export CLASSPATH=.:./jars/ij.jar:./jars/nl-mean.jar:/opt/share/java/kawa-2.0.1.jar
>> 	java -Dplugins.dir=./jars kawa.repl
>> 		(import (ij-core) (ij-nl-mean))
>> 		(define iplus1 (ij-open "images/nlm-small.png"))
>> 		(ij-nl-mean iplus1 3)
>> 
>> Good, but I would like to set this plugins.dir from kawa instead, so I can do:
>> 
>> 	export CLASSPATH=.:./jars/ij.jar:./jars/nl-mean.jar
>> 	kawa
>> 		(set! plugins.dir "./jars")
>> 		...
>> 
>> But that fails, how do I do that?

Your (set! plugins.dir "./jars") didn't work because Java system properties
are not definitions in the Scheme environment, they're entries in a special
String->String hash table maintained by the JVM.  Access to this table is
through static methods of the java.lang.System class.

> It would need some non-trivial programming.  (It could be done in Scheme,
> but it requires understand the JVM classloading mechanism, among other things.)
> It shouldn't be difficult to write a DynamicClassLoader class that searches
> for a class in a list of jars, where the list depend on plugins.dir.

Actually I don't think it's that bad.  The "plugins.dir" system property
seems to be just an ImageJ thing, not a standard property defined by the
JVM.  The ImageJ people really should have given it a more specific name,
though -- "imagej.plugins.dir" would have been a lot better.

The ImageJ documentation specifically mentions changing the property's
value programmatically:
http://rsb.info.nih.gov/ij/docs/menus/plugins.html
http://imagejdocu.tudor.lu/doku.php?id=faq:plugins:can_i_change_the_location_of_the_plugins_directory

The answer there is the same as what the answer would be for Kawa: call
System.setProperty().


So in this case, you would want to put this at the top of your program:
(java.lang.System:setProperty "plugins.dir" "./jars")


A lot of Java system properties should be considered read-only, though,
so in general Per is right, you shouldn't mess with them in a running
system, especially "java.*" or "os.*" or "sun.*".

--
Jamison Hope
The PTR Group
www.theptrgroup.com



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: java -Dplugins.dir=./jars kawa.repl
  2015-09-30 20:42   ` Jamison Hope
@ 2015-09-30 20:57     ` Per Bothner
  0 siblings, 0 replies; 10+ messages in thread
From: Per Bothner @ 2015-09-30 20:57 UTC (permalink / raw)
  To: Jamison Hope, kawa



On 09/30/2015 01:42 PM, Jamison Hope wrote:
>
> On Sep 30, 2015, at 4:24 PM, Per Bothner <per@bothner.com> wrote:
>
>> It would need some non-trivial programming.  (It could be done in Scheme,
>> but it requires understand the JVM classloading mechanism, among other things.)
>> It shouldn't be difficult to write a DynamicClassLoader class that searches
>> for a class in a list of jars, where the list depend on plugins.dir.
>
> Actually I don't think it's that bad.  The "plugins.dir" system property
> seems to be just an ImageJ thing, not a standard property defined by the
> JVM.  The ImageJ people really should have given it a more specific name,
> though -- "imagej.plugins.dir" would have been a lot better.
>
> The ImageJ documentation specifically mentions changing the property's
> value programmatically:
> http://rsb.info.nih.gov/ij/docs/menus/plugins.html
> http://imagejdocu.tudor.lu/doku.php?id=faq:plugins:can_i_change_the_location_of_the_plugins_directory
>
> The answer there is the same as what the answer would be for Kawa: call
> System.setProperty().

However, doing so will not change how *Kawa* searches for or loads classes.
This matters if you're trying to reference plugin classes from Scheme code.
In that case you have to change the "context class loader"
   Thread.currentThread().setContextClassLoader(MAGIC);
where MAGIC is a reference to the class load ImageJ uses.
You have to do this before trying to load any plugin classes into Kawa.

If you don't need to directly access plugin from Kawa (i.e. you only
need to access plugin indirectly via non-plugin code) then Jamison's
solution should work.
-- 
	--Per Bothner
per@bothner.com   http://per.bothner.com/

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: java -Dplugins.dir=./jars kawa.repl
  2015-09-30 20:25 ` Per Bothner
  2015-09-30 20:42   ` Jamison Hope
@ 2015-09-30 22:33   ` David Pirotte
  2015-09-30 23:24     ` Jamison Hope
  2015-09-30 23:45     ` Per Bothner
  1 sibling, 2 replies; 10+ messages in thread
From: David Pirotte @ 2015-09-30 22:33 UTC (permalink / raw)
  To: Per Bothner; +Cc: kawa

[-- Attachment #1: Type: text/plain, Size: 982 bytes --]

Per,
Jhope,

> ...
> My advance: Don't try to change the plugins.dir after the JVM running Kawa has
> started up.
> ...

	Thanks both

Now I want to script, here is an example:  I brutely copied the exec from guile,
I could not find an example in the manual [I may have missed if,  though]

Could you tell me what I should do instead?

	As I'm at it, does kawa has a module to parse args? I'm thinking of
	something similar to our (ice-9 getopt-long) in guile:

	https://www.gnu.org/software/guile/manual/guile.html#getopt_002dlong

Thanks,
David


;;; nl-mean starts here
#!/bin/sh
# -*- mode: scheme; coding: utf-8 -*-
exec /opt/bin/kawa -Dplugins.dir=./jars -e main -s "$0" "$@"
!#

(define (main args)
  (display (command-line))
  (newline)
  (display args)
  (newline))
;; nl-mean ends here

	chmod a+x nl-mean
	./nl-mean . img1 png img2 30
<string>:1:1: warning - no declaration seen for main
<string>:1:1: unbound location: main
...

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 473 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: java -Dplugins.dir=./jars kawa.repl
  2015-09-30 22:33   ` David Pirotte
@ 2015-09-30 23:24     ` Jamison Hope
  2015-10-01  4:32       ` David Pirotte
  2015-10-05 21:41       ` David Pirotte
  2015-09-30 23:45     ` Per Bothner
  1 sibling, 2 replies; 10+ messages in thread
From: Jamison Hope @ 2015-09-30 23:24 UTC (permalink / raw)
  To: kawa list

Note that Guile and Kawa do not take the same command-line arguments
(compare `kawa --help` and `guile --help`), so your usage of
"-e main -s" is not going to do what you're after.

You should read the Usage Reference section of the manual:
http://www.gnu.org/software/kawa/Running.html
particularly the sections on command-line arguments and "running
command scripts".

My own Kawa scripts usually start with something like this at the top:

> #!/bin/sh                                 # -*- scheme -*-
> exec java -Dkawa.command.name="$0" kawa.repl --script2 "$0" "$@"

If I need to set an environment variable or something, then I put that
before the exec and change the --scriptN argument appropriately:

> #!/bin/sh                                 # -*- scheme -*-
> export CLASSPATH=/path/to/kawa.jar:/path/to/other-stuff.jar

> exec java -Dkawa.command.name="$0" kawa.repl --script3 "$0" "$@"


--scriptN tells Kawa to disregard the first N lines of the file before
beginning to parse the file as Scheme, so you don't have to put !#
on the next line.

There is no command line argument to specify an entry point, it will
just start evaluating the contents of the file, so you don't need to
define a function called "main".

There is not a getopt facility (patches welcome), so you'll have to
iterate over the list returned by command-line or the vector
command-line-arguments.  There is a process-command-line-assignments
function which will handle simple definitions.
http://www.gnu.org/software/kawa/System-inquiry.html

Altogether, an executable Kawa script might look like:

> #!/bin/sh                                 # -*- scheme -*-
> exec java -Dkawa.command.name="$0" kawa.repl --script2 "$0" "$@"
> (let ((cl (command-line)))
>   (format #t "Hello from \"~a\"~%My arguments were:~%~{~a~%~}" (car cl)
>           (cdr cl)))

When I put that in /tmp/foo, chmod +x it, and then call it as
`/tmp/foo a b c d=3` I get:

> Hello from "/tmp/foo"
> My arguments were:
> a
> b
> c
> d=3


-Jamie

On Sep 30, 2015, at 6:33 PM, David Pirotte <david@altosw.be> wrote:

> Per,
> Jhope,
> 
>> ...
>> My advance: Don't try to change the plugins.dir after the JVM running Kawa has
>> started up.
>> ...
> 
> 	Thanks both
> 
> Now I want to script, here is an example:  I brutely copied the exec from guile,
> I could not find an example in the manual [I may have missed if,  though]
> 
> Could you tell me what I should do instead?
> 
> 	As I'm at it, does kawa has a module to parse args? I'm thinking of
> 	something similar to our (ice-9 getopt-long) in guile:
> 
> 	https://www.gnu.org/software/guile/manual/guile.html#getopt_002dlong
> 
> Thanks,
> David
> 
> 
> ;;; nl-mean starts here
> #!/bin/sh
> # -*- mode: scheme; coding: utf-8 -*-
> exec /opt/bin/kawa -Dplugins.dir=./jars -e main -s "$0" "$@"
> !#
> 
> (define (main args)
>  (display (command-line))
>  (newline)
>  (display args)
>  (newline))
> ;; nl-mean ends here
> 
> 	chmod a+x nl-mean
> 	./nl-mean . img1 png img2 30
> <string>:1:1: warning - no declaration seen for main
> <string>:1:1: unbound location: main
> ...

--
Jamison Hope
The PTR Group
www.theptrgroup.com



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: java -Dplugins.dir=./jars kawa.repl
  2015-09-30 22:33   ` David Pirotte
  2015-09-30 23:24     ` Jamison Hope
@ 2015-09-30 23:45     ` Per Bothner
  2015-10-01  2:43       ` Jamison Hope
  1 sibling, 1 reply; 10+ messages in thread
From: Per Bothner @ 2015-09-30 23:45 UTC (permalink / raw)
  To: David Pirotte; +Cc: kawa



On 09/30/2015 03:33 PM, David Pirotte wrote:
> 	As I'm at it, does kawa has a module to parse args? I'm thinking of
> 	something similar to our (ice-9 getopt-long) in guile:

Well, Kawa does support SRFI-37 (args-fold):
http://srfi.schemers.org/srfi-37/srfi-37.html
The specification is hard to read, completely lacking examples, but
the Guild docs include an example:
https://www.gnu.org/software/guile/manual/html_node/SRFI_002d37.html
Just note that Kawa uses (command-list) instead of (programming-arguements).

To use SRFI-37, do either:

(require 'srfi-37)

or:

(import (srfi 37 args-fold))

I just checked in a tweak to make the latter work, so do an 'svn update' first.

I haven't actually used args-fold myself, or studied how it works,
so I can't vouch for how helpful it is, or even if it works.
-- 
	--Per Bothner
per@bothner.com   http://per.bothner.com/

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: java -Dplugins.dir=./jars kawa.repl
  2015-09-30 23:45     ` Per Bothner
@ 2015-10-01  2:43       ` Jamison Hope
  0 siblings, 0 replies; 10+ messages in thread
From: Jamison Hope @ 2015-10-01  2:43 UTC (permalink / raw)
  To: kawa list

On Sep 30, 2015, at 7:45 PM, Per Bothner <per@bothner.com> wrote:

> Well, Kawa does support SRFI-37 (args-fold)

Oh yeah.. I think I tried to use it once and found it easier
just to roll my own.

--
Jamison Hope
The PTR Group
www.theptrgroup.com



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: java -Dplugins.dir=./jars kawa.repl
  2015-09-30 23:24     ` Jamison Hope
@ 2015-10-01  4:32       ` David Pirotte
  2015-10-05 21:41       ` David Pirotte
  1 sibling, 0 replies; 10+ messages in thread
From: David Pirotte @ 2015-10-01  4:32 UTC (permalink / raw)
  To: Jamison Hope; +Cc: kawa list

[-- Attachment #1: Type: text/plain, Size: 420 bytes --]

Hello Jamison,
Per,

> Note that Guile and Kawa do not take the same command-line arguments
> (compare `kawa --help` and `guile --help`), so your usage of
> "-e main -s" is not going to do what you're after.
> ...

Thanks for all the answers, within the next few days I'll play with the suggestions
and if necessary I'll get back to you, but I think all I need is in your repecitve
answers.

Cheers,
David

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 473 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: java -Dplugins.dir=./jars kawa.repl
  2015-09-30 23:24     ` Jamison Hope
  2015-10-01  4:32       ` David Pirotte
@ 2015-10-05 21:41       ` David Pirotte
  1 sibling, 0 replies; 10+ messages in thread
From: David Pirotte @ 2015-10-05 21:41 UTC (permalink / raw)
  To: Jamison Hope; +Cc: kawa list

[-- Attachment #1: Type: text/plain, Size: 2708 bytes --]

Hi Jamison,

	Thanks again!

For your info, here, what you proposed does not work:

#!/bin/sh
# -*- mode: scheme; coding: utf-8 -*-
exec java -Dkawa.command.name="$0" kawa.repl --script3 "$0" "$@"
(let ((cl (command-line)))
  (format #t "Hello from \"~a\"~%My arguments were:~%~{~a~%~}" (car cl)
           (cdr cl)))

->
david@capac:~/lpdi/projects/kawa 329 $ ./test a b c d=3
Error: Could not find or load main class kawa.repl

This works:

#!/bin/sh
# -*- mode: scheme; coding: utf-8 -*-
exec kawa --script4 "$0" "$@"
!#

(let ((cl (command-line)))
  (format #t "Hello from \"~a\"~%My arguments were:~%~{~a~%~}" (car cl)
           (cdr cl)))
->
david@capac:~/lpdi/projects/kawa 331 $ ./test a b c d=3
Hello from "/opt/bin/kawa --script4 ./test"
My arguments were:
a
b
c
d=3


Now, here is a small but complete kawa/imagej snipset which works as well _but_  iif
I use (exit):

	I don't know why, presumably because of imagej? not important I thought I'd
	share ...


#!/bin/sh
# -*- mode: scheme; coding: utf-8 -*-
export CLASSPATH=\
/usr/lpdi/projects/kawa:\
/usr/lpdi/projects/kawa/jars/ij.jar:\
/usr/lpdi/projects/kawa/jars/nl-mean.jar
exec kawa -Dplugins.dir=/usr/lpdi/projects/kawa/jars --script8 "$0" "$@"
!#

;;;;
;;;; Copyright (C) 2015
;;;; David Pirotte <david at altosw dot be>

;;;; This module is part of a set of image processing modules and
;;;; scripts written using Kawa and ImageJ.

;;;; This module is free software: you can redistribute it and/or modify
;;;; it under the terms of the GNU General Public License as published
;;;; by the Free Software Foundation, either version 3 of the License,
;;;; or (at your option) any later version.

;;;; It is distributed in the hope that it will be useful, but WITHOUT
;;;; ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;;; General Public License for more details.

;;;; You should have received a copy of the GNU General Public License
;;;; along with this s/w.  If not, see <http://www.gnu.org/licenses/>.
;;;;

;;; Commentary:

;;; Code:


(import (utils)
	(ij-core)
	(ij-nl-mean))


(let* ((cmd (command-line))
       (dir (list-ref cmd 1))
       (im-name (list-ref cmd 2))
       (im-type (list-ref cmd 3))
       (im-out (list-ref cmd 4))
       (sigma (string->number (list-ref cmd 5)))
       (im-filename (string-append dir "/" im-name "." im-type))
       (iplus (ij-open im-filename)))
  (ij-nl-mean iplus sigma)
  (ij-save iplus
	   (string-append dir "/" im-out "." im-type))
  (exit))


#|

 ./nl-mean . images/nlm-small png images/nlm-out 30

|#

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 473 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2015-10-05 21:41 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-09-30 19:53 java -Dplugins.dir=./jars kawa.repl David Pirotte
2015-09-30 20:25 ` Per Bothner
2015-09-30 20:42   ` Jamison Hope
2015-09-30 20:57     ` Per Bothner
2015-09-30 22:33   ` David Pirotte
2015-09-30 23:24     ` Jamison Hope
2015-10-01  4:32       ` David Pirotte
2015-10-05 21:41       ` David Pirotte
2015-09-30 23:45     ` Per Bothner
2015-10-01  2:43       ` Jamison Hope

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).