Tabnine Logo
JarFile
Code IndexAdd Tabnine to your IDE (free)

How to use
JarFile
in
java.util.jar

Best Java code snippets using java.util.jar.JarFile (Showing top 20 results out of 15,597)

Refine searchRefine arrow

  • JarEntry
  • Enumeration
  • URL
  • Attributes
  • Manifest
  • JarURLConnection
  • InputStream
  • ZipEntry
origin: apache/incubator-shardingsphere

private static Map<String, SQLCase> loadSQLCasesFromJar(final String path, final File file) throws IOException, JAXBException {
  Map<String, SQLCase> result = new TreeMap<>();
  try (JarFile jar = new JarFile(file)) {
    Enumeration<JarEntry> entries = jar.entries();
    while (entries.hasMoreElements()) {
      String name = entries.nextElement().getName();
      if (name.startsWith(path + "/") && name.endsWith(".xml")) {
        fillSQLMap(result, SQLCasesLoader.class.getClassLoader().getResourceAsStream(name));
      }
    }
  }
  return result;
}

origin: Atmosphere/atmosphere

private void loadJarContent(JarURLConnection url, String packageName, Set<InputStream> streams) throws IOException {
  // Using a JarURLConnection will load the JAR from the cache when using Webstart 1.6
  // In Webstart 1.5, the URL will point to the cached JAR on the local filesystem
  JarFile jarFile = url.getJarFile();
  Enumeration<JarEntry> entries = jarFile.entries();
  while (entries.hasMoreElements()) {
    JarEntry entry = entries.nextElement();
    if (entry.getName().startsWith(packageName)) {
      streams.add(jarFile.getInputStream(entry));
    }
  }
}
origin: jenkinsci/jenkins

protected String identifyPluginShortName(File t) {
  try {
    JarFile j = new JarFile(t);
    try {
      String name = j.getManifest().getMainAttributes().getValue("Short-Name");
      if (name!=null) return name;
    } finally {
      j.close();
    }
  } catch (IOException e) {
    LOGGER.log(WARNING, "Failed to identify the short name from "+t,e);
  }
  return FilenameUtils.getBaseName(t.getName());    // fall back to the base name of what's uploaded
}
origin: gocd/gocd

private static String getManifestKey(JarFile jarFile, String key) {
  try {
    Manifest manifest = jarFile.getManifest();
    if (manifest != null) {
      Attributes attributes = manifest.getMainAttributes();
      return attributes.getValue(key);
    }
  } catch (IOException e) {
    LOG.error("Exception while trying to read key {} from manifest of {}", key, jarFile.getName(), e);
  }
  return null;
}
origin: redisson/redisson

public InputStream openClassfile(String classname)
  throws NotFoundException
{
  try {
    String jarname = classname.replace('.', '/') + ".class";
    JarEntry je = jarfile.getJarEntry(jarname);
    if (je != null)
      return jarfile.getInputStream(je);
    else
      return null;    // not found
  }
  catch (IOException e) {}
  throw new NotFoundException("broken jar file?: "
                + jarfile.getName());
}
origin: shuzheng/zheng

jf = new JarFile(fileName);
for (Enumeration<JarEntry> e = jf.entries(); e.hasMoreElements(); ) {
  JarEntry je = e.nextElement();
  String outFileName = outputPath + je.getName();
  File f = new File(outFileName);
  if (je.isDirectory()) {
    if (!f.exists()) {
      f.mkdirs();
      pf.mkdirs();
    InputStream in = jf.getInputStream(je);
    OutputStream out = new BufferedOutputStream(
        new FileOutputStream(f));
    byte[] buffer = new byte[2048];
    int nBytes;
    while ((nBytes = in.read(buffer)) > 0) {
      out.write(buffer, 0, nBytes);
    in.close();
if (jf != null) {
  try {
    jf.close();
  } catch (IOException e) {
    e.printStackTrace();
origin: org.eclipse.jetty/jetty-webapp

/**
 * Find all .tld files in the given jar.
 * 
 * @param uri the uri to jar file
 * @return the collection of tlds as url references  
 * @throws IOException if unable to scan the jar file
 */
public Collection<URL> getTlds (URI uri) throws IOException
{
  HashSet<URL> tlds = new HashSet<URL>();
  String jarUri = uriJarPrefix(uri, "!/");
  URL url = new URL(jarUri);
  JarURLConnection jarConn = (JarURLConnection) url.openConnection();
  jarConn.setUseCaches(Resource.getDefaultUseCaches());
  JarFile jarFile = jarConn.getJarFile();
  Enumeration<JarEntry> entries = jarFile.entries();
  while (entries.hasMoreElements())
  {
    JarEntry e = entries.nextElement();
    String name = e.getName();
    if (name.startsWith("META-INF") && name.endsWith(".tld"))
    {
      tlds.add(new URL(jarUri + name));
    }
  }
  if (!Resource.getDefaultUseCaches())
    jarFile.close();
  return tlds;
}
origin: stackoverflow.com

 java.util.jar.JarFile jar = new java.util.jar.JarFile(jarFile);
java.util.Enumeration enumEntries = jar.entries();
while (enumEntries.hasMoreElements()) {
  java.util.jar.JarEntry file = (java.util.jar.JarEntry) enumEntries.nextElement();
  java.io.File f = new java.io.File(destDir + java.io.File.separator + file.getName());
  if (file.isDirectory()) { // if its a directory, create it
    f.mkdir();
    continue;
  }
  java.io.InputStream is = jar.getInputStream(file); // get the input stream
  java.io.FileOutputStream fos = new java.io.FileOutputStream(f);
  while (is.available() > 0) {  // write contents of 'is' to 'fos'
    fos.write(is.read());
  }
  fos.close();
  is.close();
}
origin: kilim/kilim

  public static void analyzeJar(String jarFile) {
    try {
      Enumeration<JarEntry> e = new JarFile(jarFile).entries();
      while (e.hasMoreElements()) {
        ZipEntry en = (ZipEntry) e.nextElement();
        String n = en.getName();
        if (!n.endsWith(".class")) continue;
        n = n.substring(0, n.length() - 6).replace('/','.');
        analyzeClass(n);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
origin: ming1016/study

public static void copyJarResourcesRecursively(File destination, JarURLConnection jarConnection) {
  JarFile jarFile;
  try {
    jarFile = jarConnection.getJarFile();
  } catch (Exception e) {
    _.die("Failed to get jar file)");
  Enumeration<JarEntry> em = jarFile.entries();
  while (em.hasMoreElements()) {
    JarEntry entry = em.nextElement();
    if (entry.getName().startsWith(jarConnection.getEntryName())) {
      String fileName = StringUtils.removeStart(entry.getName(), jarConnection.getEntryName());
      if (!fileName.equals("/")) {  // exclude the directory
        InputStream entryInputStream = null;
        try {
          entryInputStream = jarFile.getInputStream(entry);
          FileUtils.copyInputStreamToFile(entryInputStream, new File(destination, fileName));
        } catch (Exception e) {
          if (entryInputStream != null) {
            try {
              entryInputStream.close();
            } catch (Exception e) {
origin: androidannotations/androidannotations

JarFile jar = null;
try {
  jarURL = new URL("file:/" + file.getCanonicalPath());
  jarURL = new URL("jar:" + jarURL.toExternalForm() + "!/");
  JarURLConnection conn = (JarURLConnection) jarURL.openConnection();
  jar = conn.getJarFile();
} catch (Exception e) {
Enumeration<JarEntry> e = jar.entries();
while (e.hasMoreElements()) {
  JarEntry entry = e.nextElement();
  if (entry.isDirectory()) {
    if (entry.getName().toUpperCase().equals("META-INF/")) {
      continue;
      map.put(new URL(jarURL.toExternalForm() + entry.getName()), packageNameFor(entry));
    } catch (MalformedURLException murl) {
origin: androidannotations/androidannotations

File directory = new File(url.getFile());
    JarURLConnection conn = (JarURLConnection) url.openConnection();
    JarFile jarFile = conn.getJarFile();
    Enumeration<JarEntry> e = jarFile.entries();
    while (e.hasMoreElements()) {
      JarEntry entry = e.nextElement();
      String entryname = entry.getName();
      if (!entry.isDirectory() && entryname.endsWith(".class")) {
        String classname = entryname.substring(0, entryname.length() - 6);
        if (classname.startsWith("/")) {
origin: pxb1988/dex2jar

Manifest input = jar.getManifest();
Manifest output = new Manifest();
Attributes main = output.getMainAttributes();
if (input != null) {
  main.putAll(input.getMainAttributes());
main.putValue("Manifest-Version", "1.0");
main.putValue("Created-By", "1.6.0_21 (d2j-" + AbstractJarSign.class.getPackage().getImplementationVersion() + ")");
for (Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements();) {
  JarEntry entry = e.nextElement();
  byName.put(entry.getName(), entry);
  String name = entry.getName();
  if (!entry.isDirectory() && !name.equals(JarFile.MANIFEST_NAME) && !stripPattern.matcher(name).matches()) {
    InputStream data = jar.getInputStream(entry);
    while ((num = data.read(buffer)) > 0) {
      md.update(buffer, 0, num);
origin: stackoverflow.com

 JarFile jarFile = new JarFile(pathToJar);
Enumeration<JarEntry> e = jarFile.entries();

URL[] urls = { new URL("jar:file:" + pathToJar+"!/") };
URLClassLoader cl = URLClassLoader.newInstance(urls);

while (e.hasMoreElements()) {
  JarEntry je = e.nextElement();
  if(je.isDirectory() || !je.getName().endsWith(".class")){
    continue;
  }
  // -6 because of .class
  String className = je.getName().substring(0,je.getName().length()-6);
  className = className.replace('/', '.');
  Class c = cl.loadClass(className);

}
origin: spring-projects/spring-framework

  throws IOException {
URLConnection con = rootDirURL.openConnection();
JarFile jarFile;
String jarFileUrl;
  jarFile = jarCon.getJarFile();
  jarFileUrl = jarCon.getJarFileURL().toExternalForm();
  JarEntry jarEntry = jarCon.getJarEntry();
  rootEntryPath = (jarEntry != null ? jarEntry.getName() : "");
  closeJarFile = !jarCon.getUseCaches();
  String urlFile = rootDirURL.getFile();
  try {
    int separatorIndex = urlFile.indexOf(ResourceUtils.WAR_URL_SEPARATOR);
      jarFile = new JarFile(urlFile);
      jarFileUrl = urlFile;
      rootEntryPath = "";
  for (Enumeration<JarEntry> entries = jarFile.entries(); entries.hasMoreElements();) {
    JarEntry entry = entries.nextElement();
    String entryPath = entry.getName();
    if (entryPath.startsWith(rootEntryPath)) {
      String relativePath = entryPath.substring(rootEntryPath.length());
    jarFile.close();
origin: lets-blade/blade

List<FileMeta> getResourceListing(URL dirURL, String path) throws IOException {
  List<FileMeta> listFiles = new ArrayList<>();
  if (null == CACHE_JAR_FILE) {
    //strip out only the JAR file
    String jarPath = dirURL.getPath().substring(5, dirURL.getPath().indexOf("!"));
    CACHE_JAR_FILE = new JarFile(URLDecoder.decode(jarPath, "UTF-8"));
  }
  Enumeration<JarEntry> entries = CACHE_JAR_FILE.entries();
  while (entries.hasMoreElements()) {
    JarEntry jarEntry = entries.nextElement();
    String   name     = jarEntry.getName();
    if (!name.startsWith(path) || name.equals(path + "/")) {
      continue;
    }
    FileMeta fileMeta = new FileMeta();
    fileMeta.name = name.substring(path.length() + 1);
    fileMeta.isDirectory = jarEntry.isDirectory();
    if (!fileMeta.isDirectory) {
      fileMeta.length = jarEntry.getSize();
    }
    listFiles.add(fileMeta);
  }
  return listFiles;
}
origin: apache/storm

|| part.toLowerCase().endsWith(".zip")) {
try (JarFile jf = new JarFile(p.toFile())) {
  Enumeration<? extends ZipEntry> zipEnums = jf.entries();
  while (zipEnums.hasMoreElements()) {
    ZipEntry entry = zipEnums.nextElement();
    if (!entry.isDirectory() && entry.getName().equals(propFileName)) {
      try (InputStreamReader reader = new InputStreamReader(jf.getInputStream(entry))) {
        Properties info = new Properties();
        info.load(reader);
origin: eclipse-vertx/vert.x

 private static List<JavaFileObject> browseJar(URL packageFolderURL) {
  List<JavaFileObject> result = new ArrayList<>();
  try {
   String jarUri = packageFolderURL.toExternalForm().split("!")[0];
   JarURLConnection jarConn = (JarURLConnection) packageFolderURL.openConnection();
   String rootEntryName = jarConn.getEntryName();
   int rootEnd = rootEntryName.length() + 1;

   Enumeration<JarEntry> entryEnum = jarConn.getJarFile().entries();
   while (entryEnum.hasMoreElements()) {
    JarEntry jarEntry = entryEnum.nextElement();
    String name = jarEntry.getName();
    if (name.startsWith(rootEntryName) && name.indexOf('/', rootEnd) == -1 && name.endsWith(CLASS_FILE)) {
     String binaryName = name.replaceAll("/", ".").replaceAll(CLASS_FILE + "$", "");
     result.add(new CustomJavaFileObject(URI.create(jarUri + "!/" + name), JavaFileObject.Kind.CLASS, binaryName));
    }
   }
  } catch (Exception e) {
   throw new RuntimeException(packageFolderURL + " is not a JAR file", e);
  }
  return result;
 }
}
origin: stackoverflow.com

 final String path = "sample/folder";
final File jarFile = new File(getClass().getProtectionDomain().getCodeSource().getLocation().getPath());

if(jarFile.isFile()) {  // Run with JAR file
  final JarFile jar = new JarFile(jarFile);
  final Enumeration<JarEntry> entries = jar.entries(); //gives ALL entries in jar
  while(entries.hasMoreElements()) {
    final String name = entries.nextElement().getName();
    if (name.startsWith(path + "/")) { //filter according to the path
      System.out.println(name);
    }
  }
  jar.close();
} else { // Run with IDE
  final URL url = Launcher.class.getResource("/" + path);
  if (url != null) {
    try {
      final File apps = new File(url.toURI());
      for (File app : apps.listFiles()) {
        System.out.println(app);
      }
    } catch (URISyntaxException ex) {
      // never happens
    }
  }
}
origin: btraceio/btrace

private byte[] loadWithSecurity(String path) throws IOException {
  URL scriptUrl = URLClassLoader.getSystemResource(path);
  if (scriptUrl.getProtocol().equals("jar")) {
    String jarPath = scriptUrl.getPath().substring(5, scriptUrl.getPath().indexOf("!"));
    JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8"));
    Enumeration<JarEntry> ens = jar.entries();
    while (ens.hasMoreElements()) {
      JarEntry en = ens.nextElement();
      if (!en.isDirectory()) {
        if (en.toString().equals(path)) {
          byte[] data = readAll(jar.getInputStream(en), en.getSize());
          CodeSigner[] signers = en.getCodeSigners();
          canLoadPack = signers != null && signers.length != 0;
          return data;
        }
      }
    }
  }
  return null;
}
java.util.jarJarFile

Javadoc

JarFile is used to read jar entries and their associated data from jar files.

Most used methods

  • <init>
    Create a new JarFile from the contents of the file specified by filename.
  • entries
    Return an enumeration containing the JarEntrys contained in this JarFile.
  • getInputStream
    Return an InputStream for reading the decompressed contents of ZIP entry.
  • close
    Closes this JarFile.
  • getManifest
    Returns the Manifest object associated with this JarFileor null if no MANIFEST entry exists.
  • getJarEntry
    Return the JarEntry specified by its name or null if no such entry exists.
  • getEntry
    Return the JarEntry specified by name or null if no such entry exists.
  • getName
  • stream
  • size
  • getMetaEntriesImpl
    Returns all the ZipEntry's that relate to files in the JAR's META-INF directory.
  • readMetaEntries
    Called by the JarFile constructors, this method reads the contents of the file's META-INF/ directory
  • getMetaEntriesImpl,
  • readMetaEntries,
  • endsWithIgnoreCase,
  • finalize

Popular in Java

  • Making http requests using okhttp
  • setRequestProperty (URLConnection)
  • scheduleAtFixedRate (Timer)
  • onRequestPermissionsResult (Fragment)
  • BufferedImage (java.awt.image)
    The BufferedImage subclass describes an java.awt.Image with an accessible buffer of image data. All
  • PrintWriter (java.io)
    Wraps either an existing OutputStream or an existing Writerand provides convenience methods for prin
  • BigInteger (java.math)
    An immutable arbitrary-precision signed integer.FAST CRYPTOGRAPHY This implementation is efficient f
  • Proxy (java.net)
    This class represents proxy server settings. A created instance of Proxy stores a type and an addres
  • Random (java.util)
    This class provides methods that return pseudo-random values.It is dangerous to seed Random with the
  • StringTokenizer (java.util)
    Breaks a string into tokens; new code should probably use String#split.> // Legacy code: StringTo
  • Top plugins for WebStorm
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now