Search in sources :

Example 6 with JarOutputStream

use of java.util.jar.JarOutputStream in project hadoop by apache.

the class TestMRJobs method makeJobJarWithLib.

private Path makeJobJarWithLib(String testDir) throws FileNotFoundException, IOException {
    Path jobJarPath = new Path(testDir, "thejob.jar");
    FileOutputStream fos = new FileOutputStream(new File(jobJarPath.toUri().getPath()));
    JarOutputStream jos = new JarOutputStream(fos);
    // Have to put in real jar files or it will complain
    createAndAddJarToJar(jos, new File(new Path(testDir, "lib1.jar").toUri().getPath()));
    createAndAddJarToJar(jos, new File(new Path(testDir, "lib2.jar").toUri().getPath()));
    jos.close();
    localFs.setPermission(jobJarPath, new FsPermission("700"));
    return jobJarPath;
}
Also used : Path(org.apache.hadoop.fs.Path) FileOutputStream(java.io.FileOutputStream) JarOutputStream(java.util.jar.JarOutputStream) FsPermission(org.apache.hadoop.fs.permission.FsPermission) File(java.io.File)

Example 7 with JarOutputStream

use of java.util.jar.JarOutputStream in project hadoop by apache.

the class TestLocalJobSubmission method makeJar.

private Path makeJar(Path p) throws IOException {
    FileOutputStream fos = new FileOutputStream(new File(p.toString()));
    JarOutputStream jos = new JarOutputStream(fos);
    ZipEntry ze = new ZipEntry("test.jar.inside");
    jos.putNextEntry(ze);
    jos.write(("inside the jar!").getBytes());
    jos.closeEntry();
    jos.close();
    return p;
}
Also used : FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) JarOutputStream(java.util.jar.JarOutputStream) File(java.io.File)

Example 8 with JarOutputStream

use of java.util.jar.JarOutputStream in project hbase by apache.

the class ClassLoaderTestHelper method createJarArchive.

/**
   * Jar a list of files into a jar archive.
   *
   * @param archiveFile the target jar archive
   * @param tobejared a list of files to be jared
   */
private static boolean createJarArchive(File archiveFile, File[] tobeJared) {
    try {
        byte[] buffer = new byte[BUFFER_SIZE];
        // Open archive file
        FileOutputStream stream = new FileOutputStream(archiveFile);
        JarOutputStream out = new JarOutputStream(stream, new Manifest());
        for (int i = 0; i < tobeJared.length; i++) {
            if (tobeJared[i] == null || !tobeJared[i].exists() || tobeJared[i].isDirectory()) {
                continue;
            }
            // Add archive entry
            JarEntry jarAdd = new JarEntry(tobeJared[i].getName());
            jarAdd.setTime(tobeJared[i].lastModified());
            out.putNextEntry(jarAdd);
            // Write file to archive
            FileInputStream in = new FileInputStream(tobeJared[i]);
            while (true) {
                int nRead = in.read(buffer, 0, buffer.length);
                if (nRead <= 0)
                    break;
                out.write(buffer, 0, nRead);
            }
            in.close();
        }
        out.close();
        stream.close();
        LOG.info("Adding classes to jar file completed");
        return true;
    } catch (Exception ex) {
        LOG.error("Error: " + ex.getMessage());
        return false;
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) JarOutputStream(java.util.jar.JarOutputStream) Manifest(java.util.jar.Manifest) JarEntry(java.util.jar.JarEntry) FileInputStream(java.io.FileInputStream)

Example 9 with JarOutputStream

use of java.util.jar.JarOutputStream in project hbase by apache.

the class TestClassFinder method packageAndLoadJar.

/**
   * Makes a jar out of some class files. Unfortunately it's very tedious.
   * @param filesInJar Files created via compileTestClass.
   * @return path to the resulting jar file.
   */
private static String packageAndLoadJar(FileAndPath... filesInJar) throws Exception {
    // First, write the bogus jar file.
    String path = basePath + "jar" + jarCounter.incrementAndGet() + ".jar";
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    FileOutputStream fos = new FileOutputStream(path);
    JarOutputStream jarOutputStream = new JarOutputStream(fos, manifest);
    // Directory entries for all packages have to be added explicitly for
    // resources to be findable via ClassLoader. Directory entries must end
    // with "/"; the initial one is expected to, also.
    Set<String> pathsInJar = new HashSet<>();
    for (FileAndPath fileAndPath : filesInJar) {
        String pathToAdd = fileAndPath.path;
        while (pathsInJar.add(pathToAdd)) {
            int ix = pathToAdd.lastIndexOf('/', pathToAdd.length() - 2);
            if (ix < 0) {
                break;
            }
            pathToAdd = pathToAdd.substring(0, ix);
        }
    }
    for (String pathInJar : pathsInJar) {
        jarOutputStream.putNextEntry(new JarEntry(pathInJar));
        jarOutputStream.closeEntry();
    }
    for (FileAndPath fileAndPath : filesInJar) {
        File file = fileAndPath.file;
        jarOutputStream.putNextEntry(new JarEntry(fileAndPath.path + file.getName()));
        byte[] allBytes = new byte[(int) file.length()];
        FileInputStream fis = new FileInputStream(file);
        fis.read(allBytes);
        fis.close();
        jarOutputStream.write(allBytes);
        jarOutputStream.closeEntry();
    }
    jarOutputStream.close();
    fos.close();
    // Add the file to classpath.
    File jarFile = new File(path);
    assertTrue(jarFile.exists());
    URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
    Method method = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class });
    method.setAccessible(true);
    method.invoke(urlClassLoader, new Object[] { jarFile.toURI().toURL() });
    return jarFile.getAbsolutePath();
}
Also used : JarOutputStream(java.util.jar.JarOutputStream) Method(java.lang.reflect.Method) Manifest(java.util.jar.Manifest) JarEntry(java.util.jar.JarEntry) FileInputStream(java.io.FileInputStream) FileOutputStream(java.io.FileOutputStream) URLClassLoader(java.net.URLClassLoader) File(java.io.File) HashSet(java.util.HashSet)

Example 10 with JarOutputStream

use of java.util.jar.JarOutputStream in project hbase by apache.

the class JarFinder method createJar.

private static void createJar(File dir, File jarFile) throws IOException {
    Preconditions.checkNotNull(dir, "dir");
    Preconditions.checkNotNull(jarFile, "jarFile");
    File jarDir = jarFile.getParentFile();
    if (!jarDir.exists()) {
        if (!jarDir.mkdirs()) {
            throw new IOException(MessageFormat.format("could not create dir [{0}]", jarDir));
        }
    }
    try (FileOutputStream fos = new FileOutputStream(jarFile);
        JarOutputStream jos = new JarOutputStream(fos)) {
        jarDir(dir, "", jos);
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) JarOutputStream(java.util.jar.JarOutputStream) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) File(java.io.File)

Aggregations

JarOutputStream (java.util.jar.JarOutputStream)244 FileOutputStream (java.io.FileOutputStream)151 File (java.io.File)126 JarEntry (java.util.jar.JarEntry)99 Manifest (java.util.jar.Manifest)72 ZipEntry (java.util.zip.ZipEntry)56 IOException (java.io.IOException)53 JarFile (java.util.jar.JarFile)46 Test (org.junit.Test)43 FileInputStream (java.io.FileInputStream)35 InputStream (java.io.InputStream)34 ByteArrayOutputStream (java.io.ByteArrayOutputStream)33 ByteArrayInputStream (java.io.ByteArrayInputStream)27 JarInputStream (java.util.jar.JarInputStream)26 BufferedOutputStream (java.io.BufferedOutputStream)22 OutputStream (java.io.OutputStream)21 Path (java.nio.file.Path)21 ArrayList (java.util.ArrayList)20 URL (java.net.URL)13 HashMap (java.util.HashMap)13