FileUtil { public static String[] createJarWithClassPath(String inputClassPath, Path pwd, Map<String, String> callerEnv) throws IOException { return createJarWithClassPath(inputClassPath, pwd, pwd, callerEnv); }  static Path[] stat2Paths(FileStatus[] stats); static Path[] stat2Paths(FileStatus[] stats, Path path); static boolean fullyDelete(final File dir); static boolean fullyDelete(final File dir, boolean tryGrantPermissions); static String readLink(File f); static boolean fullyDeleteContents(final File dir); static boolean fullyDeleteContents(final File dir, final boolean tryGrantPermissions); @Deprecated static void fullyDelete(FileSystem fs, Path dir); static boolean copy(FileSystem srcFS, Path src,
                             FileSystem dstFS, Path dst,
                             boolean deleteSource,
                             Configuration conf); static boolean copy(FileSystem srcFS, Path[] srcs,
                             FileSystem dstFS, Path dst,
                             boolean deleteSource,
                             boolean overwrite, Configuration conf); static boolean copy(FileSystem srcFS, Path src,
                             FileSystem dstFS, Path dst,
                             boolean deleteSource,
                             boolean overwrite,
                             Configuration conf); static boolean copy(FileSystem srcFS, FileStatus srcStatus,
                             FileSystem dstFS, Path dst,
                             boolean deleteSource,
                             boolean overwrite,
                             Configuration conf); static boolean copyMerge(FileSystem srcFS, Path srcDir,
                                  FileSystem dstFS, Path dstFile,
                                  boolean deleteSource,
                                  Configuration conf, String addString); static boolean copy(File src,
                             FileSystem dstFS, Path dst,
                             boolean deleteSource,
                             Configuration conf); static boolean copy(FileSystem srcFS, Path src,
                             File dst, boolean deleteSource,
                             Configuration conf); static String makeShellPath(String filename); static String makeShellPath(File file); static String makeShellPath(File file, boolean makeCanonicalPath); static long getDU(File dir); static void unZip(File inFile, File unzipDir); static void unTar(File inFile, File untarDir); static int symLink(String target, String linkname); static int chmod(String filename, String perm
                          ); static int chmod(String filename, String perm, boolean recursive); static void setOwner(File file, String username,
      String groupname); static boolean setReadable(File f, boolean readable); static boolean setWritable(File f, boolean writable); static boolean setExecutable(File f, boolean executable); static boolean canRead(File f); static boolean canWrite(File f); static boolean canExecute(File f); static void setPermission(File f, FsPermission permission
                                   ); static final File createLocalTempFile(final File basefile,
                                               final String prefix,
                                               final boolean isDeleteOnExit); static void replaceFile(File src, File target); static File[] listFiles(File dir); static String[] list(File dir); static String[] createJarWithClassPath(String inputClassPath, Path pwd,
      Map<String, String> callerEnv); static String[] createJarWithClassPath(String inputClassPath, Path pwd,
      Path targetDir,
      Map<String, String> callerEnv); static boolean compareFs(FileSystem srcFs, FileSystem destFs); static final int SYMLINK_NO_PRIVILEGE; }
@Test (timeout = 30000) public void testCreateJarWithClassPath() throws Exception { Assert.assertFalse(tmp.exists()); Assert.assertTrue(tmp.mkdirs()); List<File> wildcardMatches = Arrays.asList(new File(tmp, "wildcard1.jar"), new File(tmp, "wildcard2.jar"), new File(tmp, "wildcard3.JAR"), new File(tmp, "wildcard4.JAR")); for (File wildcardMatch: wildcardMatches) { Assert.assertTrue("failure creating file: " + wildcardMatch, wildcardMatch.createNewFile()); } Assert.assertTrue(new File(tmp, "text.txt").createNewFile()); Assert.assertTrue(new File(tmp, "executable.exe").createNewFile()); Assert.assertTrue(new File(tmp, "README").createNewFile()); String wildcardPath = tmp.getCanonicalPath() + File.separator + "*"; String nonExistentSubdir = tmp.getCanonicalPath() + Path.SEPARATOR + "subdir" + Path.SEPARATOR; List<String> classPaths = Arrays.asList("", "cp1.jar", "cp2.jar", wildcardPath, "cp3.jar", nonExistentSubdir); String inputClassPath = StringUtils.join(File.pathSeparator, classPaths); String[] jarCp = FileUtil.createJarWithClassPath(inputClassPath + File.pathSeparator + "unexpandedwildcard/*", new Path(tmp.getCanonicalPath()), System.getenv()); String classPathJar = jarCp[0]; assertNotEquals("Unexpanded wildcard was not placed in extra classpath", jarCp[1].indexOf("unexpanded"), -1); JarFile jarFile = null; try { jarFile = new JarFile(classPathJar); Manifest jarManifest = jarFile.getManifest(); Assert.assertNotNull(jarManifest); Attributes mainAttributes = jarManifest.getMainAttributes(); Assert.assertNotNull(mainAttributes); Assert.assertTrue(mainAttributes.containsKey(Attributes.Name.CLASS_PATH)); String classPathAttr = mainAttributes.getValue(Attributes.Name.CLASS_PATH); Assert.assertNotNull(classPathAttr); List<String> expectedClassPaths = new ArrayList<String>(); for (String classPath: classPaths) { if (classPath.length() == 0) { continue; } if (wildcardPath.equals(classPath)) { for (File wildcardMatch: wildcardMatches) { expectedClassPaths.add(wildcardMatch.toURI().toURL() .toExternalForm()); } } else { File fileCp = null; if(!new Path(classPath).isAbsolute()) { fileCp = new File(tmp, classPath); } else { fileCp = new File(classPath); } if (nonExistentSubdir.equals(classPath)) { expectedClassPaths.add(fileCp.toURI().toURL() .toExternalForm() + Path.SEPARATOR); } else { expectedClassPaths.add(fileCp.toURI().toURL() .toExternalForm()); } } } List<String> actualClassPaths = Arrays.asList(classPathAttr.split(" ")); Collections.sort(expectedClassPaths); Collections.sort(actualClassPaths); Assert.assertEquals(expectedClassPaths, actualClassPaths); } finally { if (jarFile != null) { try { jarFile.close(); } catch (IOException e) { LOG.warn("exception closing jarFile: " + classPathJar, e); } } } }