1

Scenario: I have java classes in the folder in a drive. I want to invoke a method, from a specific class, say Regex. Can I use Reflection API to invoke method ? I have the following code:

File dir = new File("H:/TestFolder/sample");
    File[] directoryListing = dir.listFiles();
    if (directoryListing != null) {
        for (File child : directoryListing) {
            File[] dirListing = child.listFiles();
            for (File fileName : dirListing) {
                if (fileName.getName().equals("Regex.java")) {
                    Class<?> clazz;
                    try {

                        clazz = Class.forName("Regex");
                        Constructor<?> ctor = clazz.getConstructor();
                        Object regexClass = ctor.newInstance();
....}

I get ClassNotFoundException. Can someone tell me what am I doing wrong?

NewLearner
  • 41
  • 9
  • First of all classes cannot be load from Java source files it has to be compiled. If you want to load compiled class files from a folder check [this](http://stackoverflow.com/questions/6219829/method-to-dynamically-load-java-class-files) – seenukarthi Apr 03 '16 at 07:40
  • I would check that `Regex.class` is in your class path in the default package. – Peter Lawrey Apr 03 '16 at 07:50
  • @PeterLawrey Sir, don't you think that the missing package name in the `Class.forName(..)` is causing this? I have mentioned this in the answer section. Kindly correct me if I am mistaken. – Debosmit Ray Apr 03 '16 at 07:59
  • @DebosmitRay it could be, however if the source was `H:/TestFolder/sample` the package would have to be `sample` – Peter Lawrey Apr 03 '16 at 08:17
  • 1
    @PeterLawrey Oh yes. I missed that. I will update the answer. Thanks, sir. – Debosmit Ray Apr 03 '16 at 08:19
  • @DebosmitRay assuming the first package is not `TestFolder` ;) – Peter Lawrey Apr 03 '16 at 08:20
  • BTW Your is a complicated way of saying `Object regexClass = new Regex();` If that doesn't work, your code won't work either. – Peter Lawrey Apr 03 '16 at 08:22
  • 1
    @PeterLawrey Oh yes. Absolutely. Please do make any edits that you deem necessary. – Debosmit Ray Apr 03 '16 at 08:25

2 Answers2

1

The class is actually called [packageName].Regex because you've declared it within some package called [packageName].

Edit. Peter Lawrey pointed out that if the source was H:/TestFolder/sample the package would have to be sample. So the class will be called sample.Regex, not Regex. This is assuming that the base package isn't TestFolder. If it is, then TestFolder.sample.Regex. I'm sure you get the idea. Please read Mr. Lawrey's comment below.

Class.forName() take a class name that specifies the package name. From [docs] for the className variable,

className - the fully qualified name of the desired class.

This specificity should take of the ClassNotFoundException.

Note. I'm expecting that your Regex.java has been compiled to generate a Regex.class file. Your IDE should have done this.

Community
  • 1
  • 1
Debosmit Ray
  • 5,228
  • 2
  • 27
  • 43
  • To know which package it is, look at the `package xxx;` line at the start of the `.java` file. If there is not such line, it is in the default package. – Peter Lawrey Apr 03 '16 at 08:21
  • I don't have a package. It's the JavaProject name. It is Sample (Sorry for the typo sample). – NewLearner Apr 04 '16 at 02:59
  • So, you have no package stated at the top of your file, or is it called `sample` or `Sample`? It would be nice if you update it in the original post. **Sidenote**. If you don't have a package listed, it is good practice to have a package, instead of using `(default package)`. – Debosmit Ray Apr 04 '16 at 04:23
0

May be is a silly question but. Did you checked that these classes are in the classpath?

I mean. Is that folder the src of your code or is it an external folder you want to inspect and load?

Classes should be in the classpath in order to initalizate their objects via reflection.

Laiv
  • 306
  • 3
  • 21