5

I am working on a project of sorts to load HTML files from a server and display them in swing.

import java.io.*;
import java.net.*;
import java.util.regex.*;
import javax.swing.*;


public class webloader {
    public static void loadcode(){
        URL url = null;
        try {
            url = new URL("web"+File.separator+web.url+File.separator+"index.html");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        URLConnection con = null;
        try {
            con = url.openConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Pattern p = Pattern.compile("text/html;\\s+charset=([^\\s]+)\\s*");
        Matcher m = p.matcher(con.getContentType());
        String charset = m.matches() ? m.group(1) : "ISO-8859-1";
        Reader r = null;
        try {
            r = new InputStreamReader(con.getInputStream(), charset);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        StringBuilder buf = new StringBuilder();
        while (true) {
          int ch = 0;
        try {
            ch = r.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
          if (ch < 0)
            break;
          buf.append((char) ch);
        }
        String str = buf.toString();
        JFrame mainframe = new JFrame(web.url);
        mainframe.setSize(800, 750);
        mainframe.setResizable(false);
        JPanel website = new JPanel();
        JLabel webcontent = new JLabel(str);
        website.add(webcontent);
        mainframe.add(website);
        mainframe.setVisible(true);
    }
}

Error:

Loading test.com
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation
roblem:
        Syntax error on token ""web"", delete this token

        at webloader.loadcode(webloader.java:11)
        at web$1.actionPerformed(web.java:46)
        at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
        at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Sou
ce)
        at java.awt.Component.processMouseEvent(Unknown Source)
        at javax.swing.JComponent.processMouseEvent(Unknown Source)
        at java.awt.Component.processEvent(Unknown Source)
        at java.awt.Container.processEvent(Unknown Source)
        at java.awt.Component.dispatchEventImpl(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Window.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$200(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sou
ce)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sou
ce)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sou
ce)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)

I am quite new to Java, so if I seem to be stupid or not know what I am doing, that's because I am.

Mat
  • 202,337
  • 40
  • 393
  • 406
Airis
  • 191
  • 1
  • 1
  • 9
  • 1
    `"Error: Unresolved compilation problem:"`: You're trying to run code that won't compile. **Never** do this. Instead try to compile it first, and then if still confused, show us the *compiler's* error message, not that of the JVM. – Hovercraft Full Of Eels Dec 08 '12 at 12:27
  • There is no compiler error, – Airis Dec 08 '12 at 12:29
  • Even the error from above tells you what's wrong; you're using a variable called web, which is not declared (as far as I can tell). – Ben van Gompel Dec 08 '12 at 12:30
  • JAR export finished with warnings. See details for additional information. Exported with compile warnings: browser/src/updated.java Exported with compile warnings: browser/src/Checkupdate.java Exported with compile warnings: browser/src/web.java – Airis Dec 08 '12 at 12:31
  • Where do you see that, Ben? I don't believe I have a variable "web". – Airis Dec 08 '12 at 12:32
  • This line; url = new URL("web"+File.separator+web.url+File.separator+"index.html"); There's a "web.url" – Ben van Gompel Dec 08 '12 at 12:32
  • Witch is defined in the 'web' class, it pulls the text from the input box, witch in this case is test.com, then its SUPPOSE to go into web/test.com/index.html – Airis Dec 08 '12 at 12:34
  • Please follow standard Java conventions; classes start with a capital. I'll drop your code into Eclipse and see what's what. Even though it is not complete. – Ben van Gompel Dec 08 '12 at 12:41
  • Ok, sorry, I kinda am bad with the proper 'grammer' in coding. – Airis Dec 08 '12 at 12:46
  • 1
    That "proper grammar" is quite important if you are asking volunteers to help you. We usually appreciate the effort you put in to make it easier to understand your code, especially when you're asking for our effort to help you. It's not too much I don't think for us to ask you to clean this up and make your code comply with standards. – Hovercraft Full Of Eels Dec 08 '12 at 12:48
  • Ok, I will work on that, my apologies. – Airis Dec 08 '12 at 12:50
  • Assuming I have a class called 'web' with a public member (*shiver*) called 'url' of type String; this compiles just fine. – Ben van Gompel Dec 08 '12 at 13:07
  • Indeed, then why is this not loading properly? – Airis Dec 08 '12 at 13:08
  • Try executing a full, clean build. – Ben van Gompel Dec 08 '12 at 13:25
  • This is the full build, I don't know what you mean. Do you want me to post the other parts of my code? – Airis Dec 08 '12 at 13:26

1 Answers1

7

I was trying to access the file wrong.

Correct code:

import java.io.*;
import java.net.*;
import java.util.regex.*;
import javax.swing.*;


public class webloader {
    static JComponent page;
    public static void loadcode(){
        JEditorPane jep = new JEditorPane();
         jep.setEditable(false);   

         try {
           jep.setPage("http://(server):(port)/" + web.url);
         }
         catch (IOException e) {
           jep.setContentType("text/html");
           jep.setText("<html>Could not load webpage</html>");
         } 

         JScrollPane scrollPane = new JScrollPane(jep);     
         JFrame f = new JFrame(web.url);
         f.getContentPane().add(scrollPane);
         f.setSize(512, 342);
         f.show();
    }
}
Airis
  • 191
  • 1
  • 1
  • 9