URL for jar file

In this chapter you will learn:

  1. URL to referencing jar file in the file system
  2. How to reference a single file in a jar file

URL to referencing jar file in the file system

The following code creates a URL that refers to a jar file in the file system.

import java.net.URL;
/*from  jav a2s  .  co m*/
public class Main {
  public static void main(String[] argv) throws Exception {
    URL url = new URL("jar:file:/c://my.jar!/");
    System.out.println(url);
  }
}

The code above generates the following result.

URL for single file in a jar file

The following code creates a URL that refers to an entry in the jar file

import java.net.URL;
/* jav  a2 s  .  c o m*/
public class Main {
  public static void main(String[] argv) throws Exception {
    URL url = new URL("jar:file:/c://my.jar!/com/mycompany/MyClass.class");
    
    System.out.println(url);
  }
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. How to get URL components in Java
Home » Java Tutorial » URL URI
URL
URL Creation
URL for jar file
URL Components
Convert file path to URL
URL Relative
URL Protocol
Read from URL
Compare URL
URLConnection
HTTP Header
URLConnection Post
Cookie
URLConnection Read
HttpURLConnection
HttpURLConnection Properties
HttpURLConnection proxy
HttpURLConnection Authenticator
HTTPS
JarURLConnection
Encode a URL
Decode a URL
URI
URI Normalization
URI Resolution
URI Relativization
Convert URI to URL
IP Address
IP Ping
NetworkInterface