The Class.forName exception cannot find the class, but the parameter is the package name + class name

clipboard.png

public static <T>T newInstance(Class<T> interfaceClass){
            try {
                //    
                //  UserDao.class
                String name = interfaceClass.getSimpleName();

                //   
                String className=findClassNameByName(name);

                //
                Class<?> aClass = Class.forName(className);

                Object o = aClass.newInstance();
                return (T) o;
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }

        private static String findClassNameByName(String name) throws DocumentException {
            //
            SAXReader saxReader = new SAXReader();

            //
            InputStream in = BeanFactory.class.getClassLoader().getResourceAsStream("beans.xml");
            //doc
            Document document = saxReader.read(in);
            //xpath 
            Element element = (Element) document.selectSingleNode("//bean[@id="" + name + ""]");

            //
            return element.attributeValue("class");
        }
Mar.26,2022

  1. determine whether the class to be loaded is under classpath;
  2. if not, you can set it to classpath or load it dynamically through classloader.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e46b65-44bec.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e46b65-44bec.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?