Results 1 to 3 of 3
  1. #1
    SamSam is offline Member
    Join Date
    Feb 2009
    Rep Power

    Default Protocol for loading resources from jar

    Hi all,

    I'm writing a program that, among other things, displays html documents. These documents are often quite image-heavy, and downloading all the resources can take some time. I'm now planning on bundling some of the most commonly used resources directly into my JAR file, and want it to be easy to reference these from the external documents: e.g. instead of
    Java Code:
    <img scr="http:/"/>
    I could use
    Java Code:
    <img src="resource:/org/company/logo.jpg"/>
    or whatever. (note: only one slash shown in examples because forum doesn't like me posting links).

    Note that I may not know the location of the jar file, or maybe not even the jar file it's in. I think this should be ok, though -- I'd want to do essentially the same thing that SomeClass.getClassLoader().getResource("/org/company/...") would do.

    Apparently there's some way of creating a custom protocol, but I haven't really understood the documentation on this. Can anyone provide an example of this in action? Or, if that's not the right line of thought, some other solution?

    Thanks so much,

  2. #2
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Rep Power


    Here's an outline of the necessary steps:

    1. Create a class that implementsURLStreamHandlerFactory for your new custom protocol

    2. Register that class globally, by calling

    URL.setStreamHandlerFactory({an instance of your handler});

    Now, anytime the HTMLKit sees a URL with your custom protocol, it will call your stream handler to get a URLStreamHandler. You will need to return a customized URLStreamHandler that extends URLStreamHandler, so that you can override the openConnection methods to return a customized version of a URLConnection you've overwritten.

    Finally, you're customized URLConnection should override getInputStream to return the results of ClassLoader.getResourceAsStream.

    Painful, but doable.

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Rep Power


    Look at

    URL url = ClassLoader.getSystemClassLoader.getResource(name) ;

    This searches the classpath and returns a URL to the resource. ImageIO and File can both use URL (or URL.toURI()) to retrieve the contents of the resource.

    Since your JAR is on the classpath, your resource will be found.

    ClassLoader also has a getResourceAsStream() method.

    Note that name must use "/" to define the classpath to the resource.

Similar Threads

  1. x modem protocol
    By jithan in forum New To Java
    Replies: 0
    Last Post: 08-21-2008, 10:43 AM
  2. Loading resources generates warning messages
    By Namita Patil in forum New To Java
    Replies: 6
    Last Post: 07-03-2008, 02:04 PM
  3. Problem loading resources.
    By jimm1 in forum Advanced Java
    Replies: 6
    Last Post: 06-23-2008, 07:31 PM
  4. New Protocol?
    By lada.r in forum Networking
    Replies: 0
    Last Post: 11-06-2007, 09:13 PM
  5. how to use SIP protocol
    By katie in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 10:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts