MalformedURLException – How to solve MalformedURLException

In this example we are going to talk about It is a subclass of IOException so it is a checked exception. What you should know is that MalformedURLException is an exception that occurs when you are trying to connect to a URL from your program but your client cannot parse the URL correctly.

1. A simple HTTP client

To demonstrate that exception, we are going to to create a simple HTTP client. This client will have on method that takes as input a URL string and returns the response, e.g the HTML code of the page that we are hitting.

package com.javacodegeeks.core.lang.NumberFormatExceptionExample;


public class MalformedURLExceptionExample {

	private static final String USER_AGENT = "Mozilla/5.0";
	private static final String URL = "";

	public static void main(String[] args) {
		try {

		} catch (MalformedURLException e) {
		} catch (IOException e) {

	public static String sendGetRequest(String urlString) throws IOException {

		URL obj = new URL(urlString);
		HttpURLConnection httpConnection = (HttpURLConnection) obj


		httpConnection.setRequestProperty("User-Agent", USER_AGENT);

		int responseCode = httpConnection.getResponseCode();
		if (responseCode == 200) {

			BufferedReader responseReader = new BufferedReader(new InputStreamReader(
			String responseLine;
			StringBuffer response = new StringBuffer();

			while ((responseLine = responseReader.readLine()) != null) {

			// print result
			return response.toString();
		return null;


This will output:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""><
html xmlns="" lang="en-US" xml:lang="en-US" xmlns="" xmlns:og="" 
xmlns:fb="" ><head profile=""><link rel="stylesheet" type="text/css" 
href="" media="all" />
<script type="text/javascript" src=""></script><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>How to get the standard input in Java | Examples Java Code Geeks</title><link

As you can see, you get the normal HTTP response you’d expect.

Now. let’s see what happens if you change:

private static final String URL = "";


private static final String URL = "htp://";

I’ve changed http to htp. Let’s see what happens now: unknown protocol: htp
	at com.javacodegeeks.core.lang.NumberFormatExceptionExample.MalformedURLExceptionExample.sendGetRequest(
	at com.javacodegeeks.core.lang.NumberFormatExceptionExample.MalformedURLExceptionExample.main(

So as you can see the message that accompanies that exception is pretty informative of the cause of the problem.

In general, any URL that doesn’t follow the URL Specification cannot be parsed by this client and it will complain with a MalformedURLException.

2. How to solve MalformedURLException

In general, the message that follows the exception is usually very informative of what has gone wrong. In the previous example we’ve read unknown protocol: htp, so that means that something was wrong with the protocol we’ve chosen. Usually, it is best to log the string that you give as input to any method that might throw a MalformedURLException. That way you can be sure that the method get’s the intended input correctly. Don’t forget to read the URL Specification and make sure the URL you are providing is valid. Of course input validation, like we’ve shown in java.lang.NumberFormatException – How to solve NumberFormatException with regular expressions or any kind of input validation, is a must for very sensitive applications.

Download Source Code

This was an example on and how to solve MalformedURLException. You can download the source code of this example here :

Nikos Maravitsas

Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. During his studies he discovered his interests about software development and he has successfully completed numerous assignments in a variety of fields. Currently, his main interests are system’s security, parallel systems, artificial intelligence, operating systems, system programming, telecommunications, web applications, human – machine interaction and mobile development.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
Warvin Mogate
5 years ago

Hi Sir this is Warvin from Philippines, I would like to ask how to solve with Jasper Report in desktop application developed in JAVA Netbeans. It is developed connected to the server which ask the user to enter the URL of the server(database). when I tried to connect the application to the localhost and activate the jasperviewer, it unable to display the data instead java.netMalformedURLException. What must i do to fix this?

Back to top button