0

Here's my code

package serverloader;

import java.net.URL;
import java.net.URLClassLoader;

public class ServerLoader {

    public void initiate(String[] arguments) throws Exception {
        System.out.println("Initiating...");
        URL link = new URL("link");
        URLClassLoader classLoader = new URLClassLoader(new URL[]{link});
        Object o = classLoader.loadClass("class.MyClass").newInstance();
        System.out.println("Loaded, starting...");
    }

}

This is the loader to load my actual application, but for some reason it's not starting up and I believe it's because there are parameters that are needed to launch the application that's being loaded here, so how do I pass the parameters, which are here

String[] arguments

to the jar that is being loaded by the ClassLoader?

kbz
  • 984
  • 2
  • 12
  • 30

1 Answers1

0

Kind of, you need to use reflection to get the constructor which meets your needs, something like...

Class[] clazz = classLoader.loadClass("class.MyClass");
Constructor constructor = clazz.getConstructor(String[].class);

Once you have the constructor, you can create a new instance of the class using Constructor#newInstance...

Object o = constructor.newInstance(arguments);

As an example...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366