Solved

URL Problem

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JAVA part two 5 58
eclipse compiler vs Installed JREs option 3 77
JavaScript/Java - Changing an image background color 4 56
configure dependency in POM for new database 3 18
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

863 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

19 Experts available now in Live!

Get 1:1 Help Now