Getting a Jar File Using a URL - Java File Path IO

Java examples for File Path IO:Jar File

Description

Getting a Jar File Using a URL

Demo Code

import java.io.IOException;
import java.net.JarURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class Main {
  public static void main(String[] args) {
    try {//from w w  w .  j av  a 2 s.  co m
      URL url = new URL("jar:http://hostname/my.jar!/");

      url = new URL("jar:file:/c:/folder/my.jar!/");

      // Get the jar file
      JarURLConnection conn = (JarURLConnection) url.openConnection();
      JarFile jarfile = conn.getJarFile();

      String entryName = conn.getEntryName(); // null

      url = new URL("jar:file:/c:/folder/my.jar!/com/mycompany/MyClass.class");

      // Get the jar file
      conn = (JarURLConnection) url.openConnection();
      jarfile = conn.getJarFile();

      // Get the entry name; it should be the same as specified on URL
      entryName = conn.getEntryName();

      // Get the jar entry
      JarEntry jarEntry = conn.getJarEntry();
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    }
  }
}

Related Tutorials