Quantcast

Creating a universally accessible class library

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Creating a universally accessible class library

Rich VanMetter
I work on multiple projects, which I like to keep in separate folders of my plugins directory. However, there are common classes that I may use from all of my projects. There are many good reasons for not wanting multiple copies of these floating around, not the least of which being the ImageJ won't let me. It removes duplicate class file names from the plugins directory tree.  So a common library seems a logical solution. However, I have discovered that changing the CLASSPATH environment variable does not give me access to these classes when I compile or run plugins.

It appears that the classpath (-cp) switch in the ImageJ.cfg file overrides the more general CLASSPATH environment variable when ImageJ is run. Therefore it is necessary to include the path to the folder containing my common class library in ImageJ.cfg which now reads:

jre\bin\javaw.exe
-Xmx640m -cp "ij.jar;C:\Program Files\ImageJ\plugins\LIB" ij.ImageJ

where LIB is my library folder in the plugins directory.

I currently backup my plugins directory, since it contains much of my most valuable (difficult to replace) work. By putting the library folder there, I insure that it too is backed-up periodically. Because I do not include plugins in the LIB folder, neither the folder nor the contents appear on the plugins menu in ImageJ (a nicety). The LIB class files are however all accessible from my other plugins.

I hope others find this useful. Please correct me if I am mistaken in any of the above or if there are better ways to achieve the same ends.

Richard VanMetter
 
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Creating a universally accessible class library

dscho
Hi Richard,

On Sat, 24 Mar 2012, Richard VanMetter wrote:

> I work on multiple projects, which I like to keep in separate folders of
> my plugins directory. However, there are common classes that I may use
> from all of my projects. There are many good reasons for not wanting
> multiple copies of these floating around, not the least of which being
> the ImageJ won't let me. It removes duplicate class file names from the
> plugins directory tree.  So a common library seems a logical solution.
> However, I have discovered that changing the CLASSPATH environment
> variable does not give me access to these classes when I compile or run
> plugins.

It is very useful to collect classes you use in multiple plugins in a
common library. There are some things to keep in mind, though:

- to stay flexible, you want to bundle things in a .jar file you keep
  up-to-date.

- put the common classes in Java packages. This makes it easier to
  separate by purpose. You can then import them into classes nicely.

- if you want to use that library in plain ImageJ 1, make sure that your
  classes' names do not contain _any_ underscores. (In Fiji, this
  requirement it not necessary, as you can put the library in the jars/
  subdirectory where the classes will not be picked up as plugins by
  mistake.)

> It appears that the classpath (-cp) switch in the ImageJ.cfg file
> overrides the more general CLASSPATH environment variable when ImageJ is
> run. Therefore it is necessary to include the path to the folder
> containing my common class library

It is not a good idea to rely on that... Just put your library into
plugins/ (and jars/ in Fiji...).

> in ImageJ.cfg which now reads:
>
> jre\bin\javaw.exe
> -Xmx640m -cp "ij.jar;C:\Program Files\ImageJ\plugins\LIB" ij.ImageJ
>
> where LIB is my library folder in the plugins directory.

1) you're missing the current working directory (the first line in the
   woefully undocumented ImageJ.cfg format),

2) It is not a good idea to use absolute paths in ImageJ.cfg. At least not
   mix relative with absolute paths (a relative path would be "ij.jar",
   while an absolute path would be "C:\Program Files\ImageJ\ij.jar";
   absolute paths prevent the files from working when you move things
   between directories, or even computers)

3) instead of putting the classes into plugins/LIB/, why not put classes
   into a .jar file and putting that file directly into plugins/?

> I currently backup my plugins directory, since it contains much of my
> most valuable (difficult to replace) work.

I hear that some people put their difficult-to-replace work into a
public repository, such as an Update Site... so that they never lose it
;-)

Ciao,
Johannes
Loading...