samedi 9 juin 2018

Java reflection won't run jars with folders

I am a bit new to reflection, although I am experiencing a problem when I try to reflect a jar that contains folders or external build paths.

So this is my code for the reflection part:

File file = new File(System.getProperty("user.dir") + "\\TestingJars\\MazeWars.jar");
try {
    URLClassLoader cl = new URLClassLoader(new URL[]{file.toURI().toURL()}, null);
    Class<?> clazz = cl.loadClass("main.Driver");
    Method main = clazz.getMethod("main", String[].class);
    main.invoke(null, new Object[]{new String[]{}});
} catch (MalformedURLException | ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
    e.printStackTrace();
}

Now I am running a jar that has a folder called Assets with inside a picture. The program does work with a process and a runnable jar, or on it's own. Although I put in some sysouts before exporting to see if the folder or the file is detected and tried to run it with the above code. (the jar program is called MazeWars).

I get this:

My sysouts:

File exists: false
Directory exists: false
Other dir test exists: false

Error:

javax.imageio.IIOException: Can't read input file!
    at javax.imageio.ImageIO.read(Unknown Source)
    at GraphicalAssets.ImageLoader.loadImage(ImageLoader.java:15)
    at GraphicalAssets.Assets.initialize(Assets.java:43)
    at Environment.Maze.Maze.<init>(Maze.java:24)
    at Game.States.GameState.<init>(GameState.java:24)
    at Game.Game.initialize(Game.java:32)
    at Game.Game.run(Game.java:101)
    at java.lang.Thread.run(Unknown Source)

This is what I tried when exporting the jar:

Export generated class files and resources check with everything else unchecked

Export all output folders for checked projects checked and everything else unchecked

Export all output folders for checked projects check Export Java source files and resources check

and a combination of both of the latter

Used for export:

I am using eclipse neon I tried eclipse oxygen as well same thing





Aucun commentaire:

Enregistrer un commentaire