Solved

URL Problem

Posted on 1998-04-09
2
165 Views
Last Modified: 2010-04-16
I want to load information in to an Applet in much the same as a Windows program uses an Ini file. If I use the form
URL d = new URL("http://p1/scales/intervals");, then it works fine.

I want to make the applet work in any directory structure, so I don't need to hardcode the URL. I thought that the following would work
URL d = new URL(getCodeBase(),"intervals.txt");

Everytime I do this, I get a MalFormedURL exception.

Can you point me in the right direction with this please?
0
Comment
Question by:howesd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 6

Accepted Solution

by:
jpk041897 earned 50 total points
ID: 1234006
You misinterpreted the constructor. The following doc entry will explain this constructor better:

  public URL(URL context,
             String spec) throws MalformedURLException
 
 

 Creates a URL by parsing the specification spec  within a specified context. If the context argument  is not null and the spec argument is a  partial URL specification, then any of the strings missing  components are inherited from the context argument.  


 The specification given by the String argument is  parsed to determine if it specifies a protocol. If the  String  contains an ASCII colon ':'  character before the first occurrence of an ASCII slash character  '/ ', then the characters before the colon comprise  the protocol.  
 

· If the spec  argument does not specify a protocol:  
 

· If the context argument is not null, then the  protocol is copied from the context argument.  

· If the context argument is null, then a  MalformedURLException  is thrown.  


· If the spec argument does specify a protocol:  
 

· If the context argument is null, or specifies a   different protocol than the specification argument, the context  argument is ignored.  

· If the context argument is not null and specifies  the same protocol as the specification, the host,  port number, and file are copied from  the context argument into the newly created URL.  


 


 The constructor then searches for an appropriate stream protocol  handler of type URLStreamHandler as outlined for:  
 

  java.net.URL#URL(java.lang.String, java.lang.String, int,  java.lang.String)    


 The stream protocol handler's  parseURL method is called to parse the remaining  fields of the specification that override any defaults set by the  context argument.  


   

Parameters:  

 context - the context in which to parse the specification.  

 spec - a String representation of a URL.  

Throws: MalformedURLException  

 if no protocol is specified, or an  unknown protocol is found.  


Now what you actualy should have done was:

URL d = new URL(getCodeBase().toString + "/intervals.txt");
0
 
LVL 1

Author Comment

by:howesd
ID: 1234007
Thanks for your response - it confirmed that I was on the right lines. The problem that I was encountering actually arose as I was calling this method from within my applets constructor. As soon as I moved it to the init method it worked!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
mysql jsp example issue 32 75
ejb entity bean example 2 39
Tagging and Merging on Branch 1 44
glassfish admin console 1 23
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question