3

I’m working on an Eclipse plug-in project.

Using this plug-in, users can create Eclipse Java projects with some specificities. For example, they can add Java classes’ names which will be saved in a file. These Java classes can be created on the src of the project or used from a jar file which must be added to the project classpath. In this case, the plug-in will use reflection to get some data from each class.

There are two different test cases that give the same error because the plug-in can't find the class to instantiate:

  • A jar contain a class having a name saved in the file is not added to the project classpath. So in this case the classpath is incomplete.

  • The user of the plug-in updated a jar in which its old version of the jar contained the named class, but the new version of jar does not (which could happen if class was deleted from the new version of the jar). In this case, the plug-in will not find the class but the classpath is complete.

So The plug-in must differentiate between the two test cases when it fails finding the class name using reflection.

How that can be done?

  • 4
    You may have to add debugging statements in the code that performs reflection to print out information about the classloader and its classpath in the reflection context. This sort of thing can be tricky to debug... – FrustratedWithFormsDesigner Sep 30 '13 at 15:34
  • what does it mean "project classpath is fine"? – gnat Sep 30 '13 at 15:37
  • My goal is not to correct the error, it is to display a clear message that describes the error. "project classpath is fine" mean that all the needed jars and src are added in the .classpath file – ali Sep 30 '13 at 15:43
  • 1
    Excuse me; I was trying to simplify the question so I have not mentioned all the details. Perhaps I made a big mistake, so I will edit the question and give you all details. – ali Oct 2 '13 at 17:17
  • For the 2nd case -- has the plug in successfully found the class in a jar from a previous use of the plug-in? – Jay Elston Feb 13 '14 at 19:06
1

The only way I see for you to achieve this is to force the user to declare among the class name if the class is part of the project or part of the classpath and use that info to give the error if the reflexion fails to find the class.

I don't believe there is any other way to do this since the project it self will be part of the "classpath" and the classloader treats every class as equal

  • Actually, just a minor note: the class loader does have an order in which it uses to load classes. But you are right, he needs extra mechanism such as telling the user to declare where the class is (on src or in a jar file, give the name of that jar file) – InformedA Jun 14 '14 at 4:58
0

I see no practical difference between the two. In both cases, the class you are looking for is not on the classpath.

Perhaps, the answer is to search every drive available on the machine for every .class or .jar file. Inspect every file found to see what classes are contained inside. If the missing class is there, the classpath is incomplete. Otherwise the class doesn't exist.

This doesn't sound very realistic, though.

  • 3
    The OP believes there is a difference between the two, so I don't see how your answer addresses the question. Consider expanding your answer and explaining why you don't see a difference. – GlenH7 Oct 1 '13 at 16:15
  • The question has been edited since I originally posted the answer. But there is still no way to tell that a jar file used to contain a class in anearlier vbersion, so no real way to distinguish between the two cases. – Simon B Oct 8 '13 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.