JavaSE---Class.getResource与ClassLoader.getResource
package com.an.classloadertest; import java.io.File; import java.io.IOException; import java.net.URL; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; /** * @author apy * @description * @date 2022/3/9 16:44 */ public class TestMain { private String name; private String job; public String getJob() { return job; } public String getName() { return name; } public void setJob(String job) { this.job = job; } public void setName(String name) { this.name = name; } @Override public String toString() { return "TestMain{" + "name='" + name + '\'' + ", job='" + job + '\'' + '}'; } public static void main(String[] args) throws IOException, IllegalAccessException, InstantiationException, ClassNotFoundException { // TestMain.class.getResource("/") 与 testMain.getClass().getClassLoader().getResource("") 路径一致 // test(); // classTest(); // classLoaderTest1(); // classLoaderTest(); // scanPackage(); } private static void scanPackage() throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException { TestMain testMain = new TestMain(); List<File> list = new ArrayList<>(); final String scanPackage = "com/an/classloadertest"; Enumeration<URL> urlEnumeration = testMain.getClass().getClassLoader().getResources(scanPackage); while (urlEnumeration.hasMoreElements()){ URL url = urlEnumeration.nextElement(); File file = new File(url.getFile()); list.add(file); } String[] resources = list.stream().findFirst().get().list(); for (String resource : resources){ if (resource.equals("TestMain.class")){ Class<?> testClass = Class.forName(scanPackage.replace("/", ".") + "." + resource.replace(".class", "")); Object newInstance = testClass.newInstance(); System.out.println(newInstance); // TestMain{name='null', job='null'} } } } private static void classLoaderTest1() { TestMain testMain = new TestMain(); System.out.println(testMain.getClass().getClassLoader().getResource("")); //file:/Users/an/IdeaProjects/demo/javase/target/classes/ System.out.println(testMain.getClass().getClassLoader().getResource("1.properties")); //file:/Users/an/IdeaProjects/demo/javase/target/classes/1.properties System.out.println(testMain.getClass().getClassLoader().getResource("com/an/classloadertest/2.properties")); //file:/Users/an/IdeaProjects/demo/javase/target/classes/com/an/classloadertest/2.properties System.out.println(testMain.getClass().getClassLoader().getResource("com/an/classloadertest/sub/3.properties")); //file:/Users/an/IdeaProjects/demo/javase/target/classes/com/an/classloadertest/sub/3.properties } private static void classLoaderTest() { TestMain testMain = new TestMain(); System.out.println(testMain.getClass()); //class com.an.classloadertest.TestMain System.out.println(testMain.getClass().getClassLoader()); //sun.misc.Launcher$AppClassLoader@18b4aac2 System.out.println(testMain.getClass().getClassLoader().getResource("")); //file:/Users/an/IdeaProjects/demo/javase/target/classes/ System.out.println(testMain.getClass().getClassLoader().getResource("/")); //null } private static void classTest() { System.out.println(TestMain.class.getResource("")); //file:/Users/an/IdeaProjects/demo/javase/target/classes/com/an/classloadertest/ System.out.println(TestMain.class.getResource("/")); //file:/Users/an/IdeaProjects/demo/javase/target/classes/ System.out.println(TestMain.class.getResource("2.properties")); //file:/Users/an/IdeaProjects/demo/javase/target/classes/com/an/classloadertest/2.properties System.out.println(TestMain.class.getResource("sub/3.properties")); //file:/Users/an/IdeaProjects/demo/javase/target/classes/com/an/classloadertest/sub/3.properties System.out.println(TestMain.class.getResource("/1.properties")); //file:/Users/an/IdeaProjects/demo/javase/target/classes/1.properties } private static void test() { System.out.println(TestMain.class.getResource("")); // file:/Users/an/IdeaProjects/demo/javase/target/classes/com/an/classloadertest/ System.out.println(TestMain.class.getResource("/")); // file:/Users/an/IdeaProjects/demo/javase/target/classes/ } }