2

I have a project who's structure cannot be changed. What i have to do is that

I have a main jar file that have directores

/classes
/lib
/main
/manifest

I have to call a Test1 class when i execute the main jar file. so in the manifest file i gave the path of the Main-Class : path.to.file.

I have another jar file inside the main folder that contains the actual application. What can i do to execute the jar in main folder and execute its Test2 class mentioned in the Manifest file.

Please give me a solution or any direction to work on. Any help appreciated.

Ekansh Rastogi
  • 2,418
  • 2
  • 14
  • 23

2 Answers2

1

You can not do this in the current ClassLoader, you need a new one.

If your jar named foobar.jar and you have a main-method in the class test.abc.Main this may work:

    URL[] urls={new URL("classloader:/main/foobar.jar")};
    ClassLoader parentClassLoader = this.getClass().getClassLoader();
    URLClassLoader childClassLoader=new URLClassLoader(urls,parentClassLoader);
    Class clazz = childClassLoader.loadClass("test.abc.Main");
    String[] arguments = new String[]{};
    clazz.getMethod("main", arguments.getClass()).invoke(null,arguments);
Grim
  • 1,938
  • 10
  • 56
  • 123
1
  1. You could use JarClassLoader http://www.jdotsoft.com/JarClassLoader.php if you're okay with using GPLv3'd code to make a launcher class.

  2. Extract the jars inside of the main jar to a temporary location, execute and delete upon exit

  3. Simply change the file structure, since you haven't listed any reasons for "not doing that", I don't see why not. :D

Redfoxmoon
  • 46
  • 3