1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| public static void main(String[] args) throws IOException {
URL resource = MyTest.class.getResource("/"); URL resource2 = MyTest.class.getResource(""); URL resource3 = MyTest.class.getClassLoader().getResource(""); URL resource4 = MyTest.class.getClassLoader().getResource("/");
URL resource5 = MyTest.class.getClassLoader().getClass().getResource(""); URL resource6 = MyTest.class.getClassLoader().getClass().getResource("/");
String path = MyTest.class.getProtectionDomain().getCodeSource().getLocation().getPath();
System.out.println("codesource:"+path); File f = new File(path); System.out.println("exist?"+f.exists());
String path2 = System.getProperty("java.class.path"); System.out.println("path2::"+path2);
System.out.println("class / :"+resource); System.out.println("class 空 :"+resource2); System.out.println("classloader / :"+resource4); System.out.println("classloader 空 :"+resource3);
System.out.println("loader+class/:"+resource6); System.out.println("loader+class 空:"+resource5);
if (path.endsWith(".jar")){ System.out.println("jar execute"); JarFile jarFile = new JarFile(path); Enumeration<JarEntry> entries = jarFile.entries(); while (entries.hasMoreElements()){ JarEntry jarEntry = entries.nextElement(); String name = jarEntry.getName();
if (name.startsWith("fxml/")&&name.endsWith(".txt")){ System.out.println("zhjao"); BufferedReader b = new BufferedReader(new BufferedReader(new InputStreamReader(MyTest.class.getResourceAsStream("/"+name)))); String s = b.readLine(); System.out.println(s); } } }
InputStream resourceAsStream = MyTest.class.getResourceAsStream("/config.properties"); Properties p = new Properties(); p.load(resourceAsStream); System.out.println(p);
}
|