Url in Jar

I

Ike

I have an html page, locally, which I can have appear in a JEditorPane:

JEditorPane helpPane;
helpPane.setPage(new File("myfile.html").toURL());

Which works fine, until I package everything up into a jar file. How can I
amend the second line of code, above, to operate if my html page,
"myfile.html", is in the jar? Thanks, Ike
 
J

John McGrath

I have an html page, locally, which I can have appear in a JEditorPane:

JEditorPane helpPane;
helpPane.setPage(new File("myfile.html").toURL());

Which works fine, until I package everything up into a jar file. How can
I amend the second line of code, above, to operate if my html page,
"myfile.html", is in the jar?

Use the Class or ClassLoader getResource() method to get the URL.
 
I

Ike

John McGrath said:
On 4/4/2005 at 7:43:18 PM, Ike wrote:

Use the Class or ClassLoader getResource() method to get the URL.

I'm doing something seriously wrong here. If I have the page AIML.htm in the
root of my jar file,, why would url1 remain null but not throw an exception?
Thanks, Ike

JEditorPane helpPane= new JEditorPane();
URL url1 = getClass().getResource("AIML.htm#section-set");
helpPane.setPage(url1);
 
M

Mike Schilling

Ike said:
I'm doing something seriously wrong here. If I have the page AIML.htm in
the
root of my jar file,, why would url1 remain null but not throw an
exception?
Thanks, Ike

JEditorPane helpPane= new JEditorPane();
URL url1 = getClass().getResource("AIML.htm#section-set");
helpPane.setPage(url1);

1. The resource that the classloader can find is "AIML.htm". You'll need to
append "#section-set" after finding it.

2. A agree that the Javadoc for Class.getResource() isn't very explicit
about what happens if the resource can't be found. It does say that the
call is delegated to the class's parent ClassLoader, and if you check
ClassLoader.getResource(), you'll see that it returns null if the resource
can't be found.
 
I

Ike

Mike Schilling said:
1. The resource that the classloader can find is "AIML.htm". You'll need to
append "#section-set" after finding it.

2. A agree that the Javadoc for Class.getResource() isn't very explicit
about what happens if the resource can't be found. It does say that the
call is delegated to the class's parent ClassLoader, and if you check
ClassLoader.getResource(), you'll see that it returns null if the resource
can't be found.

But Mike, you cannot do part #1 -- at least no way that I see to append a
string onto a URL. -Ike
 
M

Mike Schilling

Ike said:
But Mike, you cannot do part #1 -- at least no way that I see to append a
string onto a URL. -Ike

new URL(url1.toExternalForm() + "#section-set");
 
I

Ike

new URL(url1.toExternalForm() + "#section-set");

A yes yes! Thanks Mike. I was confused as I thought the locator (right of
'#' inclusive) was causing the URL creation to choke -- but it was the
getClass().getResource that chokes on it! Thanks so much for your help on
this! -Ike
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top