Java Reflection not working on my system - working for team members
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 (g开发者_C百科alileo) 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 variablesAny 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.
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.
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"));
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.
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"
精彩评论