English 中文(简体)
Scala——动态班加装——A类不能投放A类
原标题:Scala - Dynamic class loading - class A can not be cast to class A

我有一份载有<条码>的简便档案。

public class A {}

接着,我装上了它。

var classLoader = new URLClassLoader(Array(my_jar_file.toURI.toURL))
var clazz = classLoader.loadClass("A")

它可以装上<代码> A。 这一指挥也是ok的:

clazz.newInstance

但是,当我投了<条码>。 A:

clazz.newInstance.asInstanceOf[A]

我得出这一错误:

java.lang.ClassCastException: A cannot be cast to A

您能否帮助我?

最佳回答

你的守则意味着,在你称之为“la”的某一类Loader环境中,你有“A”,这是你从哪里获得烟雾物体的单独背景。 问题是,在两种不同的低温环境下,你有两种不同的“A”类。 从一个版本的“A”类中产生的物体不能归入另一个版本的不同类别“Loader”类。

问题回答

I experienced a very similar problem, in that I observed a ClassCastException when casting a dynamically loaded object to an interface implemented by it.
Thanks to Neil s answer, I was able to determine that the ClassCastException was caused by having different class loader contexts.

为确定这一点,我使用了<代码>URLClassLoader(URL[] urls, LevelLoader parent)的构造者,而不是URLClassLoader(URL[] urls)的构造者。





相关问题
asp.net cookie from a resource project (+ a general info)

i d like to use a class to manage a certain cookie, but not directly on the page where i have access to all the httpcookie stuff like so HttpContext.Current.Request.Cookies["CookieName"].Value;. ...

How to get instance from string in C#?

Is it possible to get the property of a class from string and then set a value? Example: string s = "label1.text"; string value = "new value"; label1.text = value; <--and some code that makes ...

Creating (boxed) primitive instance when the class is known

I need a method that returns an instance of the supplied class type. Let s assume that the supplied types are limited to such that an "empty" instance of them can be created. For instance, supplying ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

C# Class Definition

In XCode and objective c, if I want to use a UIGlassButton in interface builder, I put the following line in my header file: @class UIGlassButton; I am now using monotouch c#. Is there a similar ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....