Various mxj objects sharing data from unique java array: static not working?!

May 2, 2010 at 3:51am

Various mxj objects sharing data from unique java array: static not working?!

A while ago i posted this question :
> Is there a way for various java codes inside various mxj objects to share some datas from a unique java array ?

And nick rothwell answered me :
> Have your MXJ classes inherit from, or refer to, some class containing a static data object.

So i finally did, and it works (at the beginning), see attached patch + classes below.

But… when i change+resave one of the classes in Eclipse, then re-instantiate it in max, its data – static data inherited from the upper class – are not anymore connected with the others derivated classes sharing the data, in my patch. Even when i re-instantiate all the mxj objects at once, by delete+cmd z, it’s the same. isn’t it strange ?

The only way that I found to “reconnect” the data between the differents classes, is to restart max and reload the patch.

I need a way to edit my classes without having to restart max all the time…

I deeply hope someone have the solution,
Thanks,
Alexandre

Attachments:
  1. Image_23.png
#50123
Oct 21, 2010 at 1:21pm

I have exactly the same problem using mxj quickie.

Did you already find a solution Alexandre?

#179965
Oct 26, 2010 at 9:16pm

This is because they have different classloaders.

In short, the JVM identifies each class by qualified name and classloader. You can think of it as having multiple versions of a class of the same name, each being defined by a different loader. Classes loaded through different class loaders do not have access to each other, except for classes loaded through classloaders which are parents of their own (such as the system classloader and the bootstrap classloader).

It is not possible to reload a class within a classloader, hence Max automatically creates new classloaders when you recompile your code. You can check which classloader was used to load a class like this:

public void postclassloader() {
  post(YourClass.class.getClassLoader().toString());
}

To have a truly static data field in the sense that it is accessible from all versions of your classes during the entire duration of your Max session, create a class that lives in the max.system.class.dir instead of max.dynamic.class.dir (or so I infer from the config file comments). This could be a relatively simple container class.

#179966
Oct 27, 2010 at 2:13am

http://cycling74.com/forums/topic.php?id=29068

I’ve found some good solutions in that topic.

#179967
Nov 21, 2010 at 4:13pm

Hi there!

To have a truly static data field in the sense that it is accessible from all versions of your classes during the entire duration of your Max session, create a class that lives in the max.system.class.dir instead of max.dynamic.class.dir (or so I infer from the config file comments). This could be a relatively simple container class.

It looks like your trick is working, Hans!!
(I had given up a while ago on this, after i wrote to the the Cycling74 support with picture below, and after Ben answered me that they couldn’t resolve my problem.)
@yns : i don’t find any alternative solution to this in your topic. (?)

I had to manage the java build path of my project in Eclipse (because as this class is living in the max.system.class.dir, so OUTSIDE of my Eclipse project, then my other classes using it were not compiling correctly) I had to add my new “maxsystemclass” Eclipse project to the java build path of my standard project. (in Properties) and then i put this in the max.java.config.txt :

max.system.class.dir /2-Erdna/eclipse/MyWorkspace/maxsystemclass/bin

max.dynamic.class.dir /2-Erdna/eclipse/MyWorkspace/MyMxjClasses/bin
max.dynamic.class.dir /2-Erdna/eclipse/MyWorkspace/MxjClasses/bin

Thanks a lot!

last example code in attachment. (using a reference to static data, instead of inheritance)

Attachments:
  1. synthetic-view-of-the-problem.jpg
#179968
Nov 23, 2010 at 1:12am

Nice that it worked out. Classloader problems can be hellish.

#179969
Nov 23, 2010 at 2:01pm

I’ve tried to fix the problem in the way you suggest Alexandre (as far as I understand it) but I’m already stuck at getting the class for the “global variables” accessible by the classes referring to this class.

I created a new eclipse for the class that hosts the “gloabal variables” and tried to add this class somehow to the java build path of the project that contains the classes referring to the variables.

I tried doing this by right clicking on the project>properties>java build path>add class folder> then I selected the project folder of the Eclipse project containing the class for the gloabal variables.

I guess I’m doing something wrong here, can you please help me out?

#179970
Nov 23, 2010 at 3:10pm

Well, i’m not a pro of java, but what i did is the following, and it worked :

In eclipse :

-> two projects:
- “MyMxjClasses” for all types of classes that i use in max,
- “maxsystemclass” for the static data classes (“global” variables as you call it)

-> then I selected the project “MyMxjClasses”, then went to Files>Properties then “Java Build Path” > tab “Projects” > add… > …add your “global-variables” project, “maxsystemclass” in my case.

#179971
Nov 23, 2010 at 5:38pm

Thank you very much!

Foolishly enough I was trying to add the project in the “Laibraries” tab.

It all works fine now.

#179972

You must be logged in to reply to this topic.