views:

126

answers:

4

I am working on a team project in Java. One requirement is that we dynamically populate a drop-down menu of all classes that implement a certain interface. New classes can be added after compile time. To accomplish this we are using reflection.

Problem: All of the drop-down menus are blank on my system. I cannot for the life of me figure out why they are not populating. All other 5 team members have it working on their system.

Things I tired that didn't work:

1) Installing most recent eclipse (galileo) because rest team was using it
2) Re-install most recent java release (jdk1.6.0-17 and jre6)
3) Check PATH and JAVA_HOME variables

Any thoughts as to what else I can try or if something I did should have solved it and didn't? It is driving me crazy.

Edit: I should have been clearer that we are developing in a team. We are using SVN for version control and we are all running the exact same source code. I even tried checking out a fresh copy of the entire tree from SVN, but I had the same issue with reflection on my system while it worked for teammates.

The team created an executable jar and that ran on everyone's system fine except for mine. Everything worked for me except the reflection bit.

+6  A: 

You need to debug your application. This means you have to systematically explore possible causes of the problem. Here are some things that come to mind:

  • Could your GUI be failing rather than reflection? What if you output with System.out.println() rather than your menu?

  • Is your reflection code throwing an exception, and are you ignoring it?

  • Is your reflection code actually being called? Toss a println() in there to be sure!

  • Is the test for the interface suffering from a typo or similar error that's causing it to fail? Try finding classes that implement Serializable instead!

  • Is your reflection test running in the main thread and trying to update your GUI? You need to use SwingUtilities.invokeAndWait to get an update to the Swing worker thread.

  • You're working with Eclipse; Eclipse has a fantastic debugger. Set a breakpoint near where your main action is and then single step through the code.

Carl Smotricz
Another thing to try: verify that one of the classes that should be found can be loaded directly. Make direct reference to it in the code or if it's not normally available at compile time do a Class.forName() look-up on it. If it can't, you know it's a classloader/classpath problem. If it can, then you know it's something in the filtering.Log or println judiciously anything found.
PSpeed
I am able to instantiate the classes directly when I do not use reflection to determine which are available.
conorgil
Please present the reflection code to us so we can critique it for you. And in the meantime, create a small program to run that same code (just the reflection code) directly from a `main()` with debug prints, and see what happens.
Carl Smotricz
+2  A: 

PATH and JAVA_HOME won't help. PATH only affects dynamically-linked libraries ("native code"). JAVA_HOME is a scripting variable that happens to be used by some Java-based utilities like Ant and Tomcat; it means nothing to the Java runtime itself.

You need to be investigating the classpath, which should be specified by the -classpath option to the java command, in the Build Path in your Eclipse project properties, or in the Class-Path attribute of the main section of a JAR file if you're launching java with the -jar option.

From within your code, you should be able to list the contents of your classpath by examining the system property, "java.class.path"

System.out.println(System.getProperty("java.class.path"));
erickson
What you say is correct, but what difference would it make if the program was running with the wrong Java? In the unlikely case that he's running on an ancient JVM, his reflection code would already be failing the compiler's syntax checks, and he'd surely notice that!
Carl Smotricz
I'm not saying anything about the version of Java. In fact, I'm saying he should stop worrying about which version of Java he's running, and focus only on the classpath. Like, make sure the classes that are supposed to populate the menus dynamically are, in fact, on the classpath.
erickson
And right you are! Thanks for the clarification, that's good advice.
Carl Smotricz
A: 

Might be a long shot, but look for differences in security settings for you and your team mates. Article describing more details http://www.ibm.com/developerworks/library/j-dyn0603/ heading "Security and reflection"

Kennet
+1 (if I wasn't outta votes). Your guess is as good as anyone's!
Carl Smotricz
A: 

Problem solution:

Classpath leading to source code must have no spaces in it.

I am running windows XP and, for whatever reason, if the classpath that leads to the jar file or source code that is using reflection has any spaces in it, then the reflection fails.

I took the jar file that works for the rest of my team and ran it from C:\ on my system and the reflection worked perfectly fine.

I do not know why this is so please comment if you know what is happening.

conorgil
As mentioned by others, we'd have to see your code that is doing the search. Java doesn't have a problem dealing with classpaths with spaces usually. So it is likely something in how your search code is parsing the CLASSPATH... since it must be.
PSpeed
Must be parsing it I mean.
PSpeed