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 8631055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:09:49+00:00 2026-06-12T09:09:49+00:00

I have a class loader that loads the main class from all jar files

  • 0

I have a class loader that loads the “main” class from all jar files in the /plugins folder
this assumes that all jars have the package plugin.(plugin name) containing the class called main. each main class has a constructor called main.

the classes load successfully, but I need to know how to call the main constructor from the loaded class.

(this class/classes are loaded at runtime)

I have tried using this:

Constructor c = cls.getConstructor(Integer.class); //line 41

Plugin plug = (Plugin) c.newInstance(0);

but I get this error:

java.lang.NoSuchMethodException: plugin.myplugin.main.<init>(java.lang.Integer)  
at java.lang.Class.getConstructor0(Unknown Source)  
at java.lang.Class.getConstructor(Unknown Source)  
at hkr.classloader.PluginLoader.loadPlugins(PluginLoader.java:41)  
at hkr.core.startup.InitializeGame.inigame(InitializeGame.java:32)  
at hkr.launcher.main.LauncherMain.main(LauncherMain.java:16)  

package hackers.classloader;

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

import org.java.plugin.Plugin;

public class PluginLoader 
{
@SuppressWarnings({ "unused", "rawtypes", "resource" })
    public static void loadPlugins() throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
    {
        Class cls = null;
        int x = hackers.core.startup.InitializeGame.map.size();
        for (int i = 1; i<=x;i++)
        {
            String className = hackers.core.startup.InitializeGame.map.get(i + "");

            File file  = new File(System.getProperty("user.dir") + File.separator + "plugins" + File.separator + className + ".jar");
            URL url = null;
            try {
                url = file.toURI().toURL();
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }  
            URL[] urls = new URL[]{url};
            ClassLoader cl = new URLClassLoader(urls);

            try {
                cls = cl.loadClass("plugin." + className + ".main");
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            Constructor c = cls.getConstructor(Integer.TYPE);
            Plugin plug = (Plugin) c.newInstance(0);
        }
    }
}
  • 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-12T09:09:49+00:00Added an answer on June 12, 2026 at 9:09 am

    If your constructor takes an java.lang.Integer, from what I see, your code should work.

    But if your constructor’s sole parameter is an int, getConstructor will fail. You have to use Integer.TYPE instead of Integer.class in that case.
    I I am right, what you need to do is:

    Constructor c = cls.getConstructor(Integer.TYPE);
    

    Edit: Based on your edits and your comments, there are several problems.

    1. The class you want to load does not seem to have any explicit constructor, which means that you simply need to do cls.getConstructor()
    2. What you want to execute (public static void main), is a static method for which you normally don’t need an instance of a class. Also, I’m not sure “main” would be a good name for the reasons explained by user @Eric B.
    3. Since you want to call a method, You have to instantiate the constructor AND also call the method.

    Based on my understanding, the code you would want to execute should be something like that:

    Constructor c = cls.getConstructor(); // we get the implicit constructor without parameters
    Plugin plugin = (Plugin) c.newInstance(); // we instantiate it, no parameters
    
    Method m = cls.getDeclaredMethod("main", Integer.TYPE);
    m.invoke(plugin, 0); // we invoke the method "main" on our dynamically loaded class, with the 0 parameter.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a main application that loads up some plugins. These plugins are loaded
I have a custom class loader which extends from a URLClassLoader. I added a
I have a context loader class which loads an XML file with info on
I have a Java class that looks like this: public class My_ABC { int
I have a class (ImageLoader) that extends Sprite container and loads an external image
I have 8 divs. Each div has the class 'pre-loader'. In addition to thas
Im working with FlashDevelop and have two main projects, all pure AS3 projects. When
Note: This is an Actionscript project, not a Flex project. I have class A
I have custom assembly that is loaded on runtime. at this point i have
I am working on a library that loads files (hfd5 - pytables) into an

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.