vendredi 3 décembre 2021

Access Class at runtime with reflections from JAR file

I'm working on a correcting system for assignments for students. Let me try to break my problem down into a simple example.

The projects I want to check look like this:

  • project-name
    • src

      • Task1

        • Solution1.java
        • Solution2.java

Now I want to check if the project has a correct implementation, but I can not assure, that Solution1.java exists, so I'm using reflection to access the classes.

I'm copying a package into the src folder, so the new project looks like this.

  • project-name
    • src

      • Task1

        • Solution1.java
        • Solution2.java
        • Correction
          • Correction.java

I don't know, if the Solution.java actually exists, so I'm using reflections.

Class.forName("Task1.Solution1");

Now I'm compiling everything inside the project and run the Correction.java - file in Linux.

This works fine so far.

In order to speed up the process, I tried to create a Solution.jar file, so I don't have to compile the file for every project. When I run the jar-file I'm getting a ClassNotFound exception, when I try to access Solution1. The jar file lies within the Correction-package.

I'm starting inside the src folder, and I run:

java -jar ./Correction/Correction.jar

Does anyone know, why this might be happening?





Aucun commentaire:

Enregistrer un commentaire