0

im generating new classes using file package but when i want to get the class properties using reflection it doesn't work and it gave me the error bellow but when i refresh my package by clicking on the right button of my mouse and i click on refresh and i rerun my function it works. So now i think that i need to change my method to get those properties by using absolut path instead of name of package.

my code

import java.lang.reflect.Field;

public class Main7{
    public static void main(String[] args) throws Exception {

        Class classe = Class.forName("com.test.model.Client");
        // affiche tous les attributs
        System.out.println("Attributs -------------------------------------");
        for (Field attribut : classe.getDeclaredFields()) {
           // System.out.print("   "+Modifier.toString(attribut.getModifiers()));
            String type = attribut.getType().getName();
            if(type.contains(".")) {
                String[] tab = type.split("\\.");
                type=tab[2];
            }
            System.out.println(type+" "+attribut.getName());
        }

    }
}

error

Exception in thread "main" java.lang.ClassNotFoundException: ma.dxc.generator.model.Client
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at generatortest.Main7.main(Main7.java:16)

good result

Attributs -------------------------------------
long id
String name
String city
bnlMedx
  • 1
  • 4
  • Have you included com.test.model.Client in your classpath somewhere? The classloader needs to find the class – AminM Jun 18 '20 at 20:26
  • i tried to do that but i didn't succeed. – bnlMedx Jun 18 '20 at 22:55
  • ok run me through it.. you have a file in: proj/com/test/model/ called Client.java Where are you running from? What is your classpath? – AminM Jun 18 '20 at 23:07
  • to be more clear im using openJPA to generate classes from database by running my code from another package, but the generated classes don't show up while my code is running. – bnlMedx Jun 18 '20 at 23:20
  • I think you need to share a bit more info on how you set up openJPA so people can help you. – AminM Jun 18 '20 at 23:22
  • Do you have an idea how to get class properties using absolute path of the class ?? – bnlMedx Jun 19 '20 at 00:12

1 Answers1

0

The easiest way to get class properties is like this:

Class klass = com.test.model.Client.class;

Or simply:

Class klass = Client.class;

If you've already imported the class above.

Or if you have an intance of an object:

Class klass = obj.getClass();

Class.forName() is used to dynamically load new classes into the system. And is not necessary to obtain the class properties through reflection.

Once you have the Class object you can obtain more info like getDeclaredFields() and others.

If you want to load something outside the current classpath you could use URLClassLoader like this:

URLClassLoader loader = new URLClassLoader(new URL[] { "file://path/to/jar/or/directory"});
Class klass = loader.loadClass("com.test.model.Client");

This article may be relevant as well: How to use URLClassLoader to load a *.class file?

Class.forName() won't look beyond your classpath. Its intended to dynamically load something that has already been added to your classpath.

AminM
  • 822
  • 4
  • 11
  • I need to use reflection because at that time when I want to use the new class, it's just I file with java extension, and I need to use absolute path because classloader can't find it using package – bnlMedx Jun 19 '20 at 15:26
  • I think if the classloader can't find it. I'm not sure Class.forName() is going to help. You may want to try URLClassLoader to gain access to a class that is outside the known classpath. – AminM Jun 19 '20 at 15:33
  • FYI- "Reflection" is the act of using the Class object and its methods. Not Class.forName(). Though Class.forName() often implies reflection. – AminM Jun 19 '20 at 20:13
  • i think that i need a method that can help me to load class from string, can you help me with that? – bnlMedx Jun 20 '20 at 08:57