Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9171581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:10:56+00:00 2026-06-17T16:10:56+00:00

I have implemented a custom class loader in Scala that serves to isolate plugins

  • 0

I have implemented a custom class loader in Scala that serves to isolate plugins from the main application. At present I require a Java interface to act as the shared root object so my main application can use the plugin code.

Interface acting as shared root (I would like this to be Scala):

public interface Handler {
    public List<HandlerInfo> getHandlers();
}

Example plugin:

class MyPlugin extends Handler {
  def getHandlers: java.util.List[HandlerInfo] = // get some handlers
}

Usage in application:

val jarFile     = new File(System.getProperty("user.dir") + "/plugins/" + jarName)
val cl          = new PluginLoader(jarFile, this.getClass.getClassLoader) // my custom classloader
val classToLoad = Class.forName(className, true, cl)
val handler     = classToLoad.newInstance.asInstanceOf[Handler]
val handlers    = handler.getHandlers

This works fine, but my problem is that I have to keep this one Java class around (and the resulting build configuration). I would like instead to use a Scala trait or abstract class, like this:

trait Handler {
  def getHandlers : List[HandlerInfo]
}

Then my plugin could look like this:

class MyPlugin extends Handler {
  def getHandlers: List[HandlerInfo] = // no more java.util.List
}

But I can’t do this, because this line

val handler     = classToLoad.newInstance.asInstanceOf[Handler]

throws a ClassCastException, presumably because the Scala compiler doesn’t generate a nice clean Java interface. Is there any way around this, so I can have a Scala-only project?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:10:57+00:00Added an answer on June 17, 2026 at 4:10 pm

    The problem is not where you think it is; it’s somewhere in your class loader. The default class loader works just fine, even the Java one. Evidence:

    // File Handler.scala
    trait Handler { def getHandlers: List[String] }
    
    // File MyPlugin.scala
    class MyPlugin extends Handler { def getHandlers = List("salmon", "cod") }
    
    // File Interop.java
    public class Interop {
      public static void main(String[] args) {
        try {
          Class classToLoad = Class.forName("MyPlugin");
          Handler handler = (Handler)classToLoad.newInstance();
          System.out.println("Class loader = "+handler.getClass().getClassLoader());
          System.out.println(handler.getHandlers());
        }
        catch (Exception e) { System.out.println("Uh-oh: "+e); }
      }
    }
    

    And here we run it:

    $ java -cp .:/usr/share/scala/2.10/lib/scala-library.jar Interop
    Class loader = sun.misc.Launcher$AppClassLoader@1f3e8d89
    List(salmon, cod)
    $
    

    with the Scala List and everything, and the new class loaded by the default Java class loader.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have implemented a custom designer class which inherits from DocumentDesigner. The standard Form
I have implemented a custom ActionMapper which obtains the locale from the URI (the
In my WCF service I have implemented a custom encoder which inherits from System.ServiceModel.Channels.MessageEncoder
I'm using OSMdroid to implement a mapping application. I have implemented a custom MapTileProvider
I have implemented a crypt behavior class that can be attached to a AR
I have implemented a custom trace listener (derived from TextWriteTraceListener ) and now I
I have implemented my custom drag&drop by deriving from COleDataSource and COleDropTarget. Everythings work
I have implemented custom title bar feature using the following class CustomWindow . Other
I have implemented custom class representing an enumeration possibilities similarily to this article :
Alright. I have implemented a custom JTable model that includes the whole @Override public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.