Solved

URL Problem

Posted on 1998-04-09
2
162 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
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

776 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