Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Cannot load classes when running a plug-in
Cannot load classes when running a plug-in [message #258081] Tue, 13 January 2009 12:01 Go to next message
hadas is currently offline hadasFriend
Messages: 7
Registered: July 2009
Junior Member
Hello, everybody.
I'm a student and new in eclipse and I have a problem that I'm dealing with
for more then a week but haven't resolved yet:
I'm developing a plugin(in java) - an expension to the eclipse IDE - that
one of its features is running a unit test and generate a report of its
results.
I've added a button that let the user to choose the wanted file and to run
it with JUnit.core.run.
When I run the plugin as eclipse application the fileChooser returns the
file full path.
JUnit.core.run suppose to get a Class, so I use Class.forName() to load the
class (I generate a class name from the full path).
The problem is that Class.forName() is looking for the class in the wrong
workspace. It looks for the class in my workspace (the plugin development
workspace) and not in the user's workspace (run time workspace) and it
throws
an exception: class not found.
When I choose a unit test from my workspace, it runs it with no exception.
In addition to the problem that it doesn't find the class, the unit test
must run in its own workspace because it uses the classes of the current
project.
I don't know what to do.
I'll be happy to get some ideas.
Thanks Hadas
Re: Cannot load classes when running a plug-in [message #258107 is a reply to message #258081] Thu, 15 January 2009 00:40 Go to previous message
Walter Harley is currently offline Walter HarleyFriend
Messages: 847
Registered: July 2009
Senior Member
hadas wrote:
> Hello, everybody.
> I'm a student and new in eclipse and I have a problem that I'm dealing with
> for more then a week but haven't resolved yet:
> I'm developing a plugin(in java) - an expension to the eclipse IDE - that
> one of its features is running a unit test and generate a report of its
> results.

The quick, but flawed, answer is that you need to define your own
classloader, based on URLClassLoader.

The longer and better answer is that you need to think carefully about
exactly what classes you're trying to load in what processes, and what
the various classpaths you're dealing with are, and I think you'll
discover you're taking a bad approach right now.

The issue is that once you load a class, two things happen. First,
particularly on Windows, it is hard to unload it until you exit the
process. That means that if you're loading it in order to test it,
you're going to have to exit and restart the process in order to change
it and test it again. You probably don't want that.

Second, you are letting that class have access to everything in your
process space. For example, if it calls System.exit(), your process
will abruptly exit. If it throws an exception that you don't catch, the
thread will exit. And so forth. So loading an arbitrary user class
into your own VM is usually a no-no - ESPECIALLY if it's a class that
you're testing, that is, a class that is not already known to be safe
and correct.

So for those reasons, test cases are usually run in a separate process.
That's why you have "Run As... JUnit Test" in Eclipse: it launches a
separate process to execute JUnit in. That's also how I would recommend
you implement your feature: start a separate process and invoke JUnit
within it. An additional benefit of launching a separate process is
that you get to specify whatever classpath you want for that process.
So you can put the correct folder on that classpath and you don't need
to use a custom classloader.

As you've noticed already, the classpath that a plug-in's classes run in
includes just those plug-in classes, plus any classes in plug-ins that
it depends on. Nothing else. That's an intentional feature of OSGi,
the classloading platform that Eclipse is based on.
Previous Topic:building workspace
Next Topic:How to silence a "... malformed doc comments ..." warning?
Goto Forum:
  


Current Time: Sat Oct 05 05:03:22 GMT 2024

Powered by FUDForum. Page generated in 0.03883 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top