Java Forum / General / September 2007
Different behaviour reading static field Windows vs Linux
colibri - 18 Sep 2007 20:56 GMT I have posted this message in the Sun Developer Network on the Java Programming Forum with little feedback. The post is http://forum.java.sun.com/thread.jspa?messageID=9882939
I am trying here with the hope of receiving some help here.
I have a class B that is a subclass of class A. Class A has a protected static field. Under windows, reading this field from a static method of class B yields the desired results, meaning that the field contains the value assigned before somewhere else.
Running the same code under Linux, the field sometimes contains the correct value and sometimes the value assigned at declaration time:
It goes like this:
package packageA;
public class A { protected static Collection items = new ArrayList();
public static void addItem(Object item) { items.add(item); }
} // class A
package packageB;
public class B extends A {
public static void methodA() { System.out.println(A.modules.size()); System.out.println(B.modules.getSize()); System.out.println(modules.size()); }
} // class B
import packageA.A; import packageB.B;
public static void void main(String[] args) {
Object item;
item = new String("");
A.addItem(item); B.method();
} // main(String[])
What is even more strange, is that if I run the above example in a new project, the results are as expected. But if I run this other project I am working on, the results are as if B.items is independent of A.items. Even if I implement a method A.getItems() and call it from B.method() to store the items in a variable local to B.method(), the result is an empty ArrayList.
My first attempt was to place the A and B classes in the same package, and since the results were as expected, I changed each class to its own package and the Main class to an independent package. The results do not change in this scenario.
Your help is appreciated.
The environment is:
Netbeans 5.5 JDK 1.6.0 xubuntu (Linux 2.6.20-16-generic i686 GNU/Linux
JC
Eric Sosman - 18 Sep 2007 21:26 GMT colibri wrote On 09/18/07 15:56,:
> I have posted this message in the Sun Developer Network on the Java > Programming Forum with little feedback. The post is > http://forum.java.sun.com/thread.jspa?messageID=9882939 > > I am trying here with the hope of receiving some help here. > [...] If you would provide the code[*] we might be able to offer some help.
[*] That is, the actual code that you compile and run to observe the puzzling symptoms. The code you provided in your post -- both here and in the forum -- should have produced nothing but a flurry of compiler errors. Let's have a look at the Real Thing, okay?
 Signature Eric.Sosman@sun.com
colibri - 19 Sep 2007 02:10 GMT > [*] That is, the actual code that you compile and run > to observe the puzzling symptoms. The code you provided > in your post -- both here and in the forum -- should have > produced nothing but a flurry of compiler errors. Let's > have a look at the Real Thing, okay? Eric:
Thanks for your help. I need your orientation here, because the code is part of a framework and I think it would be impractical to paste all the code here (thousand of lines).
The code in the original post correspond to three different classes and I am sure it runs without modifications. However, I have been debugging and I noticed that the only time when the reference to the static code is giving an unexpected answer is when it is run from a call to LoginContext.login()
The call stack shows that at some point login() is calling AccessController.doPrivileged()
My guess is that the doPrivileged() might be what is affecting the results. If you can give me a clue of how I can trace/debug this I would really appreciate it. Again, why is it affecting under Linux and not under Windows?
The interesting thing is that when debugging, the "tooltip" that is being displayed by Netbeans over the static variable displays the expected value, but the actual code returns the empty collection. So I am guessing that Netbeans does not go through the doPrivileged() code to display its results during the debugging session.
As you might think, the code is complex (it uses the LoginContext, actions, etc). If you think it is necessary for me to "build" some "short" code that shows this, I will be glad to do it. I anticipate it would be too long to make it practical to post it here. So, if that is needed, where should I place it? Maybe in a .java file accessible through http?
Thanks
JC
Eric Sosman - 19 Sep 2007 02:50 GMT >> [*] That is, the actual code that you compile and run >> to observe the puzzling symptoms. The code you provided [quoted text clipped - 10 lines] > The code in the original post correspond to three different classes > and I am sure it runs without modifications. [...] Even though class `A' refers to the Collection and ArrayList classes, but does not import either of them?
Even though `class B extends A', but `A' is in a different package and is not imported?
Even though the `main' method is not inside a class?
Even though the type of `main' is `void void'?
"Without modifications," you say? Are you by any chance related to Captain Corcoran? ("What, never?" "No, never!" "What, _never_?" "Well, hardly ever!")
I am not asking for all umpty-thousand lines of the original program, but for a reduced sample that illustrates your problem. What you've provided is reduced, I'll grant, but it illustrates nothing at all because it won't even compile.
 Signature Eric Sosman esosman@ieee-dot-org.invalid
colibri - 19 Sep 2007 21:02 GMT > Even though class `A' refers to the Collection and ArrayList > classes, but does not import either of them? [...] You are right, I apologize for my stubborn attitude and appreciate the chance to learn something new.
> I am not asking for all umpty-thousand lines of the original > program, but for a reduced sample that illustrates your problem. As you said, this helped me to trace where the problem is. The issue might not belong to this forum but I will post it here for continuity. If you happen to know of a more appropriate forum, please let me know.
I decided not to post the source code just to make this post shorter, but if you think it is better to do it, please tell me and I will more than glad to do it. Please have a look at http://www.sistema.org/src/ Over there you will find a JAR file (Test.jar) that you might run using the following command:
java -jar Test.jar test.Main
The sources are under the directory named "test" and the classes (in case you prefer to use them to run the test) are under the directory named "classes". If you opt for the latter method, copy all of the classes, change to directory where you placed the "classes" directory and use the following commands for testing (below I will explain the difference between the two):
java -cp classes com.sun.star.lib.loader.Loader test.Main java -cp classes test.Main
Note that the first command only works under Linux (i.e. it produces and error under Windows). The class is the suggested loader for Linux and is supplied as part of the OpenOffice SDK.
If the program is run using the first command, you will find that the output is a "null" value. This means that the value of the static field remains unchanged after the call to login() (which is part of the JAAS framework).
Using the second command the value of "1" is displayed as expected.
The problem is that I need the loader so I can manipulate an OpenOffice file using java. Otherwise the OpenOffice binary is not found under Linux. This is documented in the document "Transparent Use of Office UNO Components" found at http://udk.openoffice.org/common/man/spec/transparentofficecomponents.html
Your help is appreciated, I have been trying to solve this issue for a week now.
JC
Eric Sosman - 20 Sep 2007 03:05 GMT > [...] > Your help is appreciated, I have been trying to solve this issue > for a week now. Okay; thanks for the real code. Fair warnings: (1) I don't expect to have time to look at it until Sunday at the earliest, and (2) my ability to spot a bogus example doesn't imply an ability to debug a real one ...
 Signature Eric Sosman esosman@ieee-dot-org.invalid
Eric Sosman - 23 Sep 2007 22:33 GMT >> [...] >> Your help is appreciated, I have been trying to solve this issue [quoted text clipped - 4 lines] > the earliest, and (2) my ability to spot a bogus example > doesn't imply an ability to debug a real one ... Okay; I finally got a chance to look at the code. The first thing I notice is that it looks nothing like what you posted originally. Well, "nothing like" may be too strong: There may be a resemblance my unskilled eyes do not detect.
My guess -- and it's only a guess, as I'm not expert on authentication matters -- is that authentication proceeds differently on Windows and Linux. Given the many other ways in which these systems differ, I would not be surprised in the least to learn that the authentication schemes differ, too. My immediate suspicion is that the Linux configuration never loads your Module class at all, so it in turn never calls Static.setField() to initialize the mysterious field. I suggest you insert a few logging calls at strategic places (something like `new Exception("hello").printStackTrace() can be quite helpful) to see whether the code you think is being run is in fact being run. If I'm right and setField() is not being called on Linux, you'll know that the authentication code is not being told to call your code.
 Signature Eric Sosman esosman@ieee-dot-org.invalid
Andrew Thompson - 19 Sep 2007 01:59 GMT >I have posted this message in the Sun Developer Network on the Java >Programming Forum with little feedback. Not surprising, given the waste of bandwidth that 'code' represented, combined with the restrictive 'conditions of use' of the Sun forums.
Try preparing an SSCCE. <http://www.physci.org/codes/sscce.html>
(And as an aside, I am betting you will discover your programming error, before the stage where you get a postable example.)
 Signature Andrew Thompson http://www.athompson.info/andrew/
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|