class="java" name="code">1.获取当前jar包在路径 String jarURL = System.getProperty("user.dir") + File.separator + System.getProperty("java.class.path"); 2.通过命令行运行 目录结构: spider --classes StartUp --lib xx.jar xx.jar xx.jar 运行 window:java -classpath "C:\test\spider\classes;C:\test\spider\lib\*" StartUp linux: java -classpath "/tmp/spider/classes:/tmp/spider/lib/*" StartUp 3.项目通过fat-jar打包成jar,加载单个文件 InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath); 4.项目通过fat-jar打包成jar,加载某路径下的所有文件xml String jarURL = System.getProperty("user.dir") + File.separator + System.getProperty("java.class.path"); JarFile jarFile = new JarFile(jarURL); Enumeration<JarEntry> e = jarFile.entries(); while (ee.hasMoreElements()) { JarEntry jarEntry = (JarEntry) e.nextElement(); if(jarEntry.getName().endsWith("xml") && jarEntry.getName().startsWith(dir)) { InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(jarEntry.getName()); ... } }
?