?
Solved

URL Problem

Posted on 1998-04-09
2
Medium Priority
?
167 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 150 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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.
Suggested Courses
Course of the Month13 days, 16 hours left to enroll

801 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