Hello everybody, I have a little problem and I'm sure that you will solve it in a minute.
In my eclipse plug in - I let the user to choose java file with the fileChooser (it returns FILE)
afterwards I want to run the test with JUnit.core.run that receive Class parameter.
How do I get the Class from the java file I've chosen?
Thank you very much
I tried your idea and still I have a problem in run time.
I use Class.forName() that gets as parameter the class name (string) and suppose to create a class from it.
But I get an exception: class not found.
I suppose that Class.forName() doesn't look in the right place for the class.
Is there a way to tell it where to look?
The classes (test cases) are part of the current project.
I thought maybe I should paste a prefix to the class name but I don't know what prefix.
The purpuse is to give JUnit.core.run() a class to run.
In a lot of examples that I saw the class is given directly:
I'm helpless - Can you please direct me?
Thank you very much
The file object has a method called getAbsolutePath(). This will give you the full name. For example, c:/workspace/com/javaranch/MyClass.class. You'll need to derive the package name from this path. Since package names typically start with com/org/net, you should be able to parse the string to get it. Then just turn the slashes into dots, remove the file extension and you've got a fully qualified class name!
Joined: Dec 26, 2008
Sorry for being a little bit annoying but I think I didn't explain myself clearly.
We develop an eclipse plug-in.
A part of the plug-in is the option to run a unit tests from eclipse (when clicking on a button that is part of our plug-in).
The unit tests that the user would want to run are the unit tests of his project ( a project that he opened in eclipse that the plug in is installed in it).
When clicking on the button, the user can choose the wanted unit test files with a fileChooser and then when clicking "run tests", the tests should run by using JUnit.core.
When I run this plug in order to test and debug it, a new eclipse application( with the plug in installed in it) is opened and the user can work: open a java project, add classes, write unit tests etc.
When the user clicks on "run tests" I convert the file names into class names by replacing "/" with "." and removing the ".java" from the file name (as you suggested and as I did before). when I send this class name into Class.forName(), it searches the class in my project workspace (the project of the plug in) and not in the user's workspace (run time workspace) and that is the reason that the class is not found.
I want the possibility to run these unit tests in run-time from the user's workspace - this is the purpse of the plug-in: giving more services for the developer that uses the plug-in.
We want to run the unit tests like this in order to generate a special report.
Maybe I don't have to use Class.forName(). Maybe there is another way to load the class in order to give it to JUnit.core.run(). And of course the unit test have to run in the user's project environment because it tests the project classes.
Sorry for this long exhausting post.
I hope that now you understand the problem and maybe now you would be able to suggest me a solution.
Thank you very very much