0

I am creating a web browser in java and am receiving the following error when I attempt to run it:

Exception in thread "main" java.net.MalformedURLException: no protocol:

I have not been able to find an answer to my particular problem but I believe it has something to do with my socket. Do I simply need to add a MalformedURLException? Any help is appreciated.

import javax.swing.*;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;

public class Browser extends JFrame {

    public JPanel addressPanel, windowPanel;
    public JLabel addressLabel;
    public JTextField textField;
    public JEditorPane windowPane;
    public JScrollPane windowScroll;
    public JButton addressButton;
    private Search search = new Search();

    public Browser() throws IOException {

        addressLabel = new JLabel(" address: ", SwingConstants.CENTER);
        textField = new JTextField("Enter a web address..");
        textField.addActionListener(search);

        addressButton = new JButton("Go");
        addressButton.addActionListener(search);

        windowPane = new JEditorPane("");
        windowPane.setContentType("text/html");
        windowPane.setEditable(false);

        addressPanel = new JPanel(new BorderLayout());
        windowPanel = new JPanel(new BorderLayout());

        addressPanel.add(addressLabel, BorderLayout.WEST);
        addressPanel.add(textField, BorderLayout.CENTER);
        addressPanel.add(addressButton, BorderLayout.EAST);

        windowScroll = new JScrollPane(windowPane);
        windowPanel.add(windowScroll);

        Container pane = getContentPane();
        pane.setLayout(new BorderLayout());

        pane.add(addressPanel, BorderLayout.NORTH);
        pane.add(windowPanel, BorderLayout.CENTER);

        setTitle("Web Browser");
        setSize(1000, 1000);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public class Search implements ActionListener {

        public void actionPerformed(ActionEvent ea) {

            String line;
            try {

                Socket socket = new Socket(textField.getText(), 80);
                PrintWriter out = new PrintWriter(socket.getOutputStream());
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                out.print("GET / HTTP/1.1\r\n");
                out.print(textField.getText() + "\r\n\r\n");
                out.flush();
                while ((line = in.readLine()) != null) {

                    System.out.println(line);
                }
            } catch (Exception e) {

                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws IOException {

        Browser browser = new Browser();
    }
}
2
  • 1
    You need to have a protocol in that url. Commented Oct 23, 2016 at 23:23
  • Show us the input you type in the text field. Commented Oct 23, 2016 at 23:47

1 Answer 1

1

The problem was because the line bellow:

windowPane = new JEditorPane("");

Just change to

windowPane = new JEditorPane();


According to JEditorPane constructor javadoc:

Creates a JEditorPane based on a string containing a URL specification.
@param url the URL
@exception IOException if the URL is null or cannot be accessed

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.