JVMUtil { public static String[] addAllJarsToClassPath(String dirName) { if (!FileUtil.isDirectory(dirName)) { return null; } File dir = new File(dirName); List<String> ret = CollectionUtil.createArrayList(); File[] files = dir.listFiles(); for (File file : files) { if (file.isDirectory()) { ret.addAll(Arrays.asList(addAllJarsToClassPath(file.getAbsolutePath()))); continue; } if (file.getName().toLowerCase().endsWith(".jar") && appendToClassPath(file.getAbsolutePath())) { ret.add(file.getAbsolutePath()); } } return ret.toArray(new String[0]); }  static boolean appendToClassPath(String name); static String[] addAllJarsToClassPath(String dirName); static Manifest getManifest(File classpathItem); static File[] getClasspath(); static File[] getClasspath(ClassLoader classLoader); static final JavaRuntimeInfo getJavaRuntimeInfo();  }
@Test public void addAllJarsToClassPath() { String libPath = classpath + File.separator + "lib"; File[] jarSuffixs = FileUtil.listDirSuffixFiles(libPath, ".jar"); String[] jars = JVMUtil.addAllJarsToClassPath(libPath); assertTrue(ArrayUtil.isNotEmpty(jars)); assertEquals(jarSuffixs.length, jars.length); for (int i = 0, size = jars.length; i < size; i++) { assertEquals(jarSuffixs[i].getAbsolutePath(), jars[i]); } assertNull(JVMUtil.addAllJarsToClassPath(null)); assertTrue(ArrayUtil.isEmpty(JVMUtil.addAllJarsToClassPath(""))); assertTrue(ArrayUtil.isEmpty(JVMUtil.addAllJarsToClassPath("notexist"))); }