473,413 Members | 1,811 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Help: ClassLoader and Casting??

I need to load a different version of a class than what is being used
by the overall system. My test code works fine for loading the class,
but fails when I try to cast the Object. For example, the following
throws a java.lang.NoClassDefFoundError when trying to cast.

public class URLTester
{
public static void main( String args[] ) throws Exception
{
// Create an array of URLs
// From directory name specified as args[0]
URL urlList[] = {new File( args[ 0 ] ).toURL()};

// Create a URLClassLoader
ClassLoader loader = new URLClassLoader( urlList );
// Thread.currentThread().setContextClassLoader( loader );

// Load class from class loader
// Use args[1] as class name
Class c = loader.loadClass( args[ 1 ] );

// Create an instance of the class just loaded
Object obj = c.newInstance();

// This fails with a NoClassDefFoundError
Tester tester = (Tester)obj;
}
}

My hunch is that the system ClassLoader is doing the casting. I've
tried, with no success, to add
Thread.currentThread().setContextClassLoader( loader ) to change the
thread's ClassLoader.

Any help would be greatly appreciated!

-- John
Jul 17 '05 #1
0 1500

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: news.dsl.pipex.com | last post by:
Greetings, I'm trying to find out how in the source you set the version/vendor/title information so that it can be read at runtime. All I've found is mention of ClassLoader in relation to...
0
by: gg | last post by:
I was playing with some free java code but I was miffed. In the current working directory, I have xyz.jar and a subdirectory coresrvlets. The classes of the package coreservlets are naturally...
10
by: Nikita A. Visnevski | last post by:
Hi everyone, I am rather new to Java beans. Just picked up a book last night and started reading about them. I am building an application that allows a user to define objects with dynamic...
1
by: John Davison | last post by:
I have an Object that was created in a custom class loader. Since a Class is defined as the class name plus the class loader, a Class from one ClassLoader is not equivalent to a Class from another...
0
by: Cruinne | last post by:
Hello, In a JBoss 3.2.4 server, I have deployed several enterprise javabeans (each as a separate .jar, not in an .ear bundle). These beans are completely self-contained, as they include all...
5
by: stephan beal | last post by:
Good afternoon, C++ers, (i hope this is not viewed as an advertisement or shameless plug, and i appologize if it is interpretted that way.) i have developed a template-based classloader which...
2
by: Don | last post by:
Can someone help me fix my DLookup problem. I'm far from proficiency with Access. I've been creating databases for several years for work with the help of many of you and trial and error. I have...
3
by: knightsmastergeneral | last post by:
Hi, I'm currently working on getting junit and junireport to take some java files, convert it to xml and then display error messages / success rates etc in html which is one of the features of...
3
by: prashfire2000 | last post by:
This is a code i wrote to play a video file usin JMF..... the applet gets started but not initialized..... is the code correct or is there any mistake?????? please help me out as soon as u...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.