Java Tutorial

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java List Interface

Java Queue Interface

Java Map Interface

Java Set Interface

Java Data Structures

Java Collections Algorithms

Advanced Java

Java Miscellaneous

Java APIs & Frameworks

Java Useful Resources

Java - URL getAuthority() Method with Examples



Description

The Java URL getAuthority() method returns the authority part of this URL. −

Declaration

Following is the declaration for java.net.URL.getAuthority() method

public String getAuthority()

Parameters

NA

Return Value

the authority part of this URL.

Exception

NA

Example 1

The following example shows the usage of Java URL getAuthority() method for a valid url. In this example, we're creating an instances of URL class. Now using getAuthority() method, we're getting the authority part of the url and printed it in output as shown below −

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;

public class UrlDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https://www.tutorialspoint.com/index.htm?language=en#j2se");      
         System.out.println(url.getAuthority());
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

www.tutorialspoint.com

Example 2

The following example shows the usage of Java URL getAuthority() method for a invalid url. In this example, we're creating an instances of URL class. Now using getAuthority() method, we're trying to get the authority part of the url and printed it in output. As url is not complete and missing the protocol, an exception is thrown by URL constructor in output as shown below −

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;

public class UrlDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("www.tutorialspoint.com");      
         System.out.println(url.getAuthority());
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

java.net.MalformedURLException: no protocol: www.tutorialspoint.com
	at java.net.URL.<init>(Unknown Source)
	at java.net.URL.<init>(Unknown Source)
	at java.net.URL.<init>(Unknown Source)
	at com.tutorialspoint.UrlDemo.main(UrlDemo.java:9)

Example 3

The following example shows the usage of Java URL getAuthority() method for a valid url. In this example, we're creating an instances of URL class using protocol, host and file name. Now using getAuthority() method, we're getting the authority part of the url and printed it in output as shown below −

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;

public class UrlDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https","www.tutorialspoint.com","index.htm");      
         System.out.println(url.getAuthority());
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

www.tutorialspoint.com
java_url.htm
Advertisements