html contained in jar

Discussion in 'Java' started by johnmmcparland, Oct 9, 2006.

  1. Hi all,

    this seems like a commonly asked question, but so far I haven't found
    an answer that works.

    I wish to have a jar file which by containing a class which extends
    JEditorPane and some html files, will display the html files which are
    located inside the jar.

    At the moment, I have a jar file with the following contents;

    META-INF/
    META-INF/MANIFEST.MF
    help/
    help/index.html
    ui/
    ui/Browser$HTMLBrowser$LinkListener.class
    ui/Browser$HTMLBrowser.class
    ui/Browser.class
    ui/Browser.java

    The Browser inner class HTMLBrowser extends JEditorPane to display html
    (in this case help/index.html).

    The code is below,

    package ui;

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

    import javax.swing.JEditorPane;
    import javax.swing.JFrame;
    import javax.swing.event.HyperlinkEvent;
    import javax.swing.event.HyperlinkListener;

    public class Browser extends JFrame
    {
    private HTMLBrowser browser;
    private static final long serialVersionUID= 1;

    public Browser()
    {
    setTitle("Help");
    browser= new HTMLBrowser();
    setContentPane(browser);
    setSize(800,600);
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setLocationRelativeTo(null);
    setVisible(true);
    }

    private class HTMLBrowser extends JEditorPane
    {
    private static final long serialVersionUID= 2;
    private static final String dir= "help";
    private static final String startPage= "index.html";

    public HTMLBrowser()
    {
    URL url;
    try
    {
    // Construct the URL
    // This is;
    // current dir + separator + help dir + separator + startPage
    String s= (new File(".")).getCanonicalPath();
    String separator= System.getProperty("file.separator");
    String t= s + separator + dir + separator + startPage;
    // DEBUG
    System.out.println(t);
    File f= new File(t);
    url= f.toURL();
    setPage(url);
    }
    catch (Exception e)
    {
    System.out.println( "Problem setting help homepage");
    }
    setEditable(false);
    addHyperlinkListener(new LinkListener(this));
    }

    private class LinkListener implements HyperlinkListener
    {
    private HTMLBrowser browser;

    public LinkListener(HTMLBrowser b)
    {
    browser= b;
    }

    public void hyperlinkUpdate(HyperlinkEvent he)
    {
    if (he.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
    {
    try
    {
    URL newPage = he.getURL();
    browser.setPage(newPage);

    }
    catch (IOException ioe)
    {
    System.out.println( "Error opening new page");
    }
    }
    }
    }

    }


    public static void main(String[] args)
    {
    new Browser();
    }

    }

    When I run this using "java Browser" (inside ui dir") it works (i.e.
    displays the html page I want)
    When I run the jar in the directory containing the "help" and "ui"
    directories it works fine.
    But when I run the jar anywhere else it does not work. The html page
    is not displayed. Instead a blank JEditorPane is all that can be seen.

    How can I get my class to look at the help/index.html file from within
    the jar?

    Thanks in advance,

    John
     
    johnmmcparland, Oct 9, 2006
    #1
    1. Advertising

  2. johnmmcparland wrote:
    ....
    > this seems like a commonly asked question, but so far I haven't found
    > an answer that works.

    ...
    > How can I get my class to look at the help/index.html file from within
    > the jar?


    getResource()

    Andrew T.
     
    Andrew Thompson, Oct 9, 2006
    #2
    1. Advertising

  3. Nope I'm afraid that using get resource as follows;

    public HTMLBrowser()
    {
    URL url;
    try
    {
    // Construct the URL
    String separator= System.getProperty("file.separator");
    String t= separator + dir + separator + startPage;
    // DEBUG
    System.out.println(t);
    url= getClass().getResource(t);
    setPage(url);
    }
    catch (Exception e)
    {
    System.out.println( "Problem setting help homepage");
    }
    setEditable(false);
    addHyperlinkListener(new LinkListener(this));
    }

    did not work.


    Andrew Thompson wrote:

    > johnmmcparland wrote:
    > ...
    > > this seems like a commonly asked question, but so far I haven't found
    > > an answer that works.

    > ..
    > > How can I get my class to look at the help/index.html file from within
    > > the jar?

    >
    > getResource()
    >
    > Andrew T.
     
    johnmmcparland, Oct 9, 2006
    #3
  4. Hmm but constructing the string as so;


    String t= "/" + dir + "/" + startPage;

    does work. Strange.


    johnmmcparland wrote:

    > Nope I'm afraid that using get resource as follows;
    >
    > public HTMLBrowser()
    > {
    > URL url;
    > try
    > {
    > // Construct the URL
    > String separator= System.getProperty("file.separator");
    > String t= separator + dir + separator + startPage;
    > // DEBUG
    > System.out.println(t);
    > url= getClass().getResource(t);
    > setPage(url);
    > }
    > catch (Exception e)
    > {
    > System.out.println( "Problem setting help homepage");
    > }
    > setEditable(false);
    > addHyperlinkListener(new LinkListener(this));
    > }
    >
    > did not work.
    >
    >
    > Andrew Thompson wrote:
    >
    > > johnmmcparland wrote:
    > > ...
    > > > this seems like a commonly asked question, but so far I haven't found
    > > > an answer that works.

    > > ..
    > > > How can I get my class to look at the help/index.html file from within
    > > > the jar?

    > >
    > > getResource()
    > >
    > > Andrew T.
     
    johnmmcparland, Oct 9, 2006
    #4
  5. johnmmcparland wrote:
    > Hmm


    Pleas refrain from top-posting - even when replying
    to your own posts..

    top-post corrected for clarity.
    > > > > How can I get my class to look at the help/index.html file from within
    > > > > the jar?
    > > >
    > > > getResource()

    ....
    >...but constructing the string as so;
    >
    > String t= "/" + dir + "/" + startPage;
    >
    > does work. Strange.


    Not so strange, really. URL's always use the '/'.

    Using the file separator is only appropriate for getting
    files off the 'file system', as opposed to resources out
    of jar archives.

    HTH

    Andrew T.
     
    Andrew Thompson, Oct 9, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arnold Peters
    Replies:
    0
    Views:
    587
    Arnold Peters
    Jan 5, 2005
  2. Roedy Green

    making sure a jar is self-contained

    Roedy Green, Jun 16, 2005, in forum: Java
    Replies:
    8
    Views:
    466
    Rogan Dawes
    Jun 23, 2005
  3. muttley
    Replies:
    0
    Views:
    2,731
    muttley
    Oct 20, 2005
  4. cyberco
    Replies:
    4
    Views:
    3,773
    Roedy Green
    Feb 14, 2006
  5. Arnold Peters
    Replies:
    0
    Views:
    670
    Arnold Peters
    Jan 5, 2005
Loading...

Share This Page