Solved

URL Problem

Posted on 1998-04-09
2
159 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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now