0

So there's a lot of questions and examples around of reading external .class files using a ClassLoader but I'm struggling to see where I'm going wrong.

val folderUrl: URL = new File("D:/tmp/").toURI.toURL //file:/D:/tmp/
val cl: URLClassLoader = new URLClassLoader(Array(folderUrl), this.getClass.getClassLoader)
cl.loadClass("my.package.MyClassName")

The last line throws a ClassNotFoundException

The folder D:/tmp/ contains a class file "MyClassName.class". The class has the package "my.package" The class is called "MyClassName"

I can't understand what I'm doing wrong?

EDIT: The two closest question which relate are:

But these both do not have my problem however, they both get further than I have done where they successfully load the class before running into issues.

Community
  • 1
  • 1
A Spoty Spot
  • 746
  • 6
  • 19
  • Possible duplicate of [How do I call a Scala Object method using reflection?](http://stackoverflow.com/questions/3039822/how-do-i-call-a-scala-object-method-using-reflection) – Rhys Bradbury Jun 07 '16 at 16:16
  • is this a java or scala class? – Rhys Bradbury Jun 07 '16 at 16:17
  • It is a scala class. The question you linked to doesn't mention any loading from a class file, I don't think it is a duplicate unless I misunderstand? – A Spoty Spot Jun 07 '16 at 16:20
  • Make sure you're using Scala Reflection and not Java Reflection. http://docs.scala-lang.org/overviews/reflection/overview.html – Rhys Bradbury Jun 07 '16 at 16:21
  • The docs there only show how to do runtime inspection of the classes, not actually load classes from files themselves which is what I'm struggling with. – A Spoty Spot Jun 07 '16 at 16:36
  • I think you need to be a bit more specific about your particular use case in your question, and how it differs from the posts in your edit – Rhys Bradbury Jun 07 '16 at 16:50
  • The first question in my edit successfully calls loadClass in his question. I cannot do that, once I've got that successfully working Ill be able to follow the answers in their questions. I can't really provide a better stack or situation of what's going since when trying to debug the classLoader code loses me. I'll try again tommorow when I'm back at my desk. – A Spoty Spot Jun 07 '16 at 16:55

1 Answers1

0

So the issue was the fact that the folder structure did not match the package name.

So my folder structure was D:/tmp/MyClassName.class The full class name was my.package.MyClassName

The class loader requires that the folder structure be D:/tmp/my/package/MyClassName.class

A Spoty Spot
  • 746
  • 6
  • 19