Java Code Examples for java.util.jar.JarFile#MANIFEST_NAME

The following examples show how to use java.util.jar.JarFile#MANIFEST_NAME . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Utils.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
    if (in.getManifest() != null) {
        ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
        out.putNextEntry(me);
        in.getManifest().write(out);
        out.closeEntry();
    }
    byte[] buffer = new byte[1 << 14];
    for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
        out.putNextEntry(je);
        for (int nr; 0 < (nr = in.read(buffer)); ) {
            out.write(buffer, 0, nr);
        }
    }
    in.close();
    markJarFile(out);  // add PACK200 comment
}
 
Example 2
Source File: Utils.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
    if (in.getManifest() != null) {
        ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
        out.putNextEntry(me);
        in.getManifest().write(out);
        out.closeEntry();
    }
    byte[] buffer = new byte[1 << 14];
    for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
        out.putNextEntry(je);
        for (int nr; 0 < (nr = in.read(buffer)); ) {
            out.write(buffer, 0, nr);
        }
    }
    in.close();
    markJarFile(out);  // add PACK200 comment
}
 
Example 3
Source File: Utils.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
    if (in.getManifest() != null) {
        ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
        out.putNextEntry(me);
        in.getManifest().write(out);
        out.closeEntry();
    }
    byte[] buffer = new byte[1 << 14];
    for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
        out.putNextEntry(je);
        for (int nr; 0 < (nr = in.read(buffer)); ) {
            out.write(buffer, 0, nr);
        }
    }
    in.close();
    markJarFile(out);  // add PACK200 comment
}
 
Example 4
Source File: JarFinder.java    From big-c with Apache License 2.0 6 votes vote down vote up
public static void jarDir(File dir, String relativePath, ZipOutputStream zos)
  throws IOException {
  Preconditions.checkNotNull(relativePath, "relativePath");
  Preconditions.checkNotNull(zos, "zos");

  // by JAR spec, if there is a manifest, it must be the first entry in the
  // ZIP.
  File manifestFile = new File(dir, JarFile.MANIFEST_NAME);
  ZipEntry manifestEntry = new ZipEntry(JarFile.MANIFEST_NAME);
  if (!manifestFile.exists()) {
    zos.putNextEntry(manifestEntry);
    new Manifest().write(new BufferedOutputStream(zos));
    zos.closeEntry();
  } else {
    copyToZipStream(manifestFile, manifestEntry, zos);
  }
  zos.closeEntry();
  zipDir(dir, relativePath, zos, true);
  zos.close();
}
 
Example 5
Source File: Utils.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
    if (in.getManifest() != null) {
        ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
        out.putNextEntry(me);
        in.getManifest().write(out);
        out.closeEntry();
    }
    byte[] buffer = new byte[1 << 14];
    for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
        out.putNextEntry(je);
        for (int nr; 0 < (nr = in.read(buffer)); ) {
            out.write(buffer, 0, nr);
        }
    }
    in.close();
    markJarFile(out);  // add PACK200 comment
}
 
Example 6
Source File: Utils.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
    if (in.getManifest() != null) {
        ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
        out.putNextEntry(me);
        in.getManifest().write(out);
        out.closeEntry();
    }
    byte[] buffer = new byte[1 << 14];
    for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
        out.putNextEntry(je);
        for (int nr; 0 < (nr = in.read(buffer)); ) {
            out.write(buffer, 0, nr);
        }
    }
    in.close();
    markJarFile(out);  // add PACK200 comment
}
 
Example 7
Source File: Utils.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
    if (in.getManifest() != null) {
        ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
        out.putNextEntry(me);
        in.getManifest().write(out);
        out.closeEntry();
    }
    byte[] buffer = new byte[1 << 14];
    for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
        out.putNextEntry(je);
        for (int nr; 0 < (nr = in.read(buffer)); ) {
            out.write(buffer, 0, nr);
        }
    }
    in.close();
    markJarFile(out);  // add PACK200 comment
}
 
Example 8
Source File: PluginEngineTargetForFolderTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testManifest() throws Exception {
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    Plugin.Engine.Target target = new Plugin.Engine.Target.ForFolder(folder);
    target.write(manifest).close();
    File file = new File(folder, JarFile.MANIFEST_NAME);
    assertThat(file.isFile(), is(true));
    InputStream inputStream = new FileInputStream(file);
    try {
        Manifest readManifest = new Manifest(inputStream);
        assertThat(readManifest.getMainAttributes().get(Attributes.Name.MANIFEST_VERSION), is((Object) "1.0"));
    } finally {
        inputStream.close();
    }
    assertThat(file.delete(), is(true));
    assertThat(file.getParentFile().delete(), is(true));
}
 
Example 9
Source File: Utils.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
    if (in.getManifest() != null) {
        ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
        out.putNextEntry(me);
        in.getManifest().write(out);
        out.closeEntry();
    }
    byte[] buffer = new byte[1 << 14];
    for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
        out.putNextEntry(je);
        for (int nr; 0 < (nr = in.read(buffer)); ) {
            out.write(buffer, 0, nr);
        }
    }
    in.close();
    markJarFile(out);  // add PACK200 comment
}
 
Example 10
Source File: MainAttributes.java    From Mixin with MIT License 6 votes vote down vote up
private static Attributes getDirAttributes(File dir) {
    File manifestFile = new File(dir, JarFile.MANIFEST_NAME);
    if (manifestFile.isFile()) {
        ByteSource source = Files.asByteSource(manifestFile);
        InputStream inputStream = null;
        try {
            inputStream = source.openBufferedStream();
            Manifest manifest = new Manifest(inputStream);
            return manifest.getMainAttributes();
        } catch (IOException ex) {
            // be quiet checkstyle
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                // ignore
            }
        }
    }
    
    return null;
}
 
Example 11
Source File: Utils.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
    if (in.getManifest() != null) {
        ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
        out.putNextEntry(me);
        in.getManifest().write(out);
        out.closeEntry();
    }
    byte[] buffer = new byte[1 << 14];
    for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
        out.putNextEntry(je);
        for (int nr; 0 < (nr = in.read(buffer)); ) {
            out.write(buffer, 0, nr);
        }
    }
    in.close();
    markJarFile(out);  // add PACK200 comment
}
 
Example 12
Source File: TestJvmUtils.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
@Test
public void findMainClass_jar_normal() throws Exception {

  URL url = PowerMockito.mock(URL.class);

  String command = "a.jar";
  String manifestUri = "jar:file:" + new File(command).getAbsolutePath() + "!/" + JarFile.MANIFEST_NAME;
  PowerMockito.whenNew(URL.class).withParameterTypes(String.class)
      .withArguments(manifestUri).thenReturn(url);

  String content = String.format("Manifest-Version: 1.0\nMain-Class: %s\n", TestJvmUtils.class.getName());
  InputStream inputStream = new ByteArrayInputStream(content.getBytes());
  PowerMockito.when(url.openStream()).thenReturn(inputStream);

  System.setProperty(JvmUtils.SUN_JAVA_COMMAND, command + " arg");

  Assert.assertEquals(TestJvmUtils.class, JvmUtils.findMainClass());
}
 
Example 13
Source File: PluginEngineSourceForFolderTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testManifest() throws Exception {
    File file = new File(folder, JarFile.MANIFEST_NAME);
    assertThat(file.getParentFile().mkdir(), is(true));
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    OutputStream outputStream = new FileOutputStream(file);
    try {
        manifest.write(outputStream);
    } finally {
        outputStream.close();
    }
    Plugin.Engine.Source.Origin origin = new Plugin.Engine.Source.ForFolder(folder).read();
    try {
        Manifest readManifest = origin.getManifest();
        assertThat(readManifest, notNullValue(Manifest.class));
        assertThat(readManifest.getMainAttributes().getValue(Attributes.Name.MANIFEST_VERSION), is("1.0"));
    } finally {
        origin.close();
    }
    assertThat(file.delete(), is(true));
    assertThat(file.getParentFile().delete(), is(true));
}
 
Example 14
Source File: Utils.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
    if (in.getManifest() != null) {
        ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
        out.putNextEntry(me);
        in.getManifest().write(out);
        out.closeEntry();
    }
    byte[] buffer = new byte[1 << 14];
    for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
        out.putNextEntry(je);
        for (int nr; 0 < (nr = in.read(buffer)); ) {
            out.write(buffer, 0, nr);
        }
    }
    in.close();
    markJarFile(out);  // add PACK200 comment
}
 
Example 15
Source File: JarFinder.java    From hbase with Apache License 2.0 6 votes vote down vote up
public static void jarDir(File dir, String relativePath, ZipOutputStream zos)
  throws IOException {
  Preconditions.checkNotNull(relativePath, "relativePath");
  Preconditions.checkNotNull(zos, "zos");

  // by JAR spec, if there is a manifest, it must be the first entry in the
  // ZIP.
  File manifestFile = new File(dir, JarFile.MANIFEST_NAME);
  ZipEntry manifestEntry = new ZipEntry(JarFile.MANIFEST_NAME);
  if (!manifestFile.exists()) {
    zos.putNextEntry(manifestEntry);
    new Manifest().write(new BufferedOutputStream(zos));
    zos.closeEntry();
  } else {
    copyToZipStream(manifestFile, manifestEntry, zos);
  }
  zos.closeEntry();
  zipDir(dir, relativePath, zos, true);
  zos.close();
}
 
Example 16
Source File: Main.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a JAR file.
 *
 * Equivalent to {@code jar cfm <jarfile> <manifest> -C <dir> file...}
 *
 * The input files are resolved against the given directory. Any input
 * files that are directories are processed recursively.
 */
static void createJarFile(Path jarfile, Manifest man, Path dir, String... files)
    throws IOException
{
    // create the target directory
    Path parent = jarfile.getParent();
    if (parent != null)
        Files.createDirectories(parent);

    List<Path> entries = new ArrayList<>();
    for (String file : files) {
        Files.find(dir.resolve(file), Integer.MAX_VALUE,
                (p, attrs) -> attrs.isRegularFile())
                .map(e -> dir.relativize(e))
                .forEach(entries::add);
    }

    try (OutputStream out = Files.newOutputStream(jarfile);
         JarOutputStream jos = new JarOutputStream(out))
    {
        if (man != null) {
            JarEntry je = new JarEntry(JarFile.MANIFEST_NAME);
            jos.putNextEntry(je);
            man.write(jos);
            jos.closeEntry();
        }

        for (Path entry : entries) {
            String name = toJarEntryName(entry);
            jos.putNextEntry(new JarEntry(name));
            Files.copy(dir.resolve(entry), jos);
            jos.closeEntry();
        }
    }
}
 
Example 17
Source File: Utils.java    From spork with Apache License 2.0 5 votes vote down vote up
public static void jarDir(File dir, String relativePath, ZipOutputStream zos)
        throws IOException {
    Preconditions.checkNotNull(relativePath, "relativePath");
    Preconditions.checkNotNull(zos, "zos");

    // by JAR spec, if there is a manifest, it must be the first entry in
    // the
    // ZIP.
    File manifestFile = new File(dir, JarFile.MANIFEST_NAME);
    ZipEntry manifestEntry = new ZipEntry(JarFile.MANIFEST_NAME);
    if (!manifestFile.exists()) {
        zos.putNextEntry(manifestEntry);
        new Manifest().write(new BufferedOutputStream(zos));
        zos.closeEntry();
    } else {
        InputStream is = new FileInputStream(manifestFile);
        try {
            copyToZipStream(is, manifestEntry, zos);
        } finally {
            if (is != null) {
                is.close();
            }
            if (zos != null) {
                zos.closeEntry();
            }
        }
    }
    zos.closeEntry();
    zipDir(dir, relativePath, zos, true);
    zos.close();
}
 
Example 18
Source File: ImmortalDappModule.java    From AVM with MIT License 5 votes vote down vote up
/**
 * Create the in-memory JAR containing all the classes in this module.
 */
public byte[] createJar(long blockTimeStamp) throws IOException {
    // set jar file timestamp to block timestamp so the whole network is in agreement over this.
    FileTime timestamp = FileTime.fromMillis(blockTimeStamp);

    // manifest, we explicitly write it so that can can control its timestamps.
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, this.mainClass);

    ZipEntry manifestEntry = new ZipEntry(JarFile.MANIFEST_NAME);
    manifestEntry.setLastModifiedTime(timestamp);
    manifestEntry.setLastAccessTime(timestamp);
    manifestEntry.setCreationTime(timestamp);

    // Create a temporary memory location for this JAR.
    ByteArrayOutputStream tempJarStream = new ByteArrayOutputStream(MAX_JAR_BYTES);

    // create the jar file
    try (JarOutputStream target = new JarOutputStream(tempJarStream)) {
        // first, write the manifest file
        target.putNextEntry(manifestEntry);
        manifest.write(target);
        target.closeEntry();

        // add the classes
        for (String clazz : this.classes.keySet()) {
            JarEntry entry = new JarEntry(clazz.replace('.', '/') + ".class");
            entry.setLastModifiedTime(timestamp);
            entry.setLastAccessTime(timestamp);
            entry.setCreationTime(timestamp);
            target.putNextEntry(entry);
            target.write(this.classes.get(clazz));
            target.closeEntry();
        }
    }
    return tempJarStream.toByteArray();
}
 
Example 19
Source File: Plugin.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Manifest getManifest() throws IOException {
    File file = new File(folder, JarFile.MANIFEST_NAME);
    if (file.exists()) {
        InputStream inputStream = new FileInputStream(file);
        try {
            return new Manifest(inputStream);
        } finally {
            inputStream.close();
        }
    } else {
        return NO_MANIFEST;
    }
}
 
Example 20
Source File: ManifestFinder.java    From spliceengine with GNU Affero General Public License v3.0 4 votes vote down vote up
ManifestFinder() {
    this.spliceJarFilePattern = "/splice_machine";
    this.manifestResourcePath = JarFile.MANIFEST_NAME;
}