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
I have exactly the same problem using mxj quickie.
Did you already find a solution Alexandre?
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.
I've found some good solutions in that topic.
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)
Nice that it worked out. Classloader problems can be hellish.
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?
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.
Thank you very much!
Foolishly enough I was trying to add the project in the "Laibraries" tab.
It all works fine now.