2

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?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
GraphMarv
  • 21
  • 2
  • How are you running your JAR file? – Mark Rotteveel Dec 03 '21 at 18:19
  • Starting inside the src folder I run: `java -jar ./Correction/Correction.jar` – GraphMarv Dec 03 '21 at 18:22
  • 1
    Well, that is your problem. When using `-jar`, the class path is entire defined by the JAR, so it doesn't see the custom classes that aren't part of the jar (and what is defined in the `Class-Path` entry in the manifest). You will need to use `java -cp .:./Correction/Correction.jar name.of.your.MainClass` (if you're on Windows, separate classpath entries with `;` instead of `:`). – Mark Rotteveel Dec 03 '21 at 18:26
  • See also: [Run a JAR file from the command line and specify classpath](https://stackoverflow.com/questions/18413014/run-a-jar-file-from-the-command-line-and-specify-classpath) – Mark Rotteveel Dec 03 '21 at 18:31
  • I'm not sure, if understood correctty. But my Main-Class is inside the jar file. I want to access the other files via reflections from within the .jar file. – GraphMarv Dec 03 '21 at 18:58
  • If I understand you correctly, you want to access classes **outside** your JAR using reflection. – Mark Rotteveel Dec 04 '21 at 06:34
  • Alright. I tried `java -cp ./Task1/*:./Correction/Correction.jar Path.To.MainClass.Inside.JAR`. But I'm still not able to acces the class Solution1. – GraphMarv Dec 04 '21 at 12:54
  • Did you actually compile `Solution1`? Does `Solution1.java` have a package statement, if so, what is its value? Is there a `Solution1.class` in the `src/Task1` directory? – Mark Rotteveel Dec 04 '21 at 12:56
  • `Solution1` is compiled and the `.class`-File lies within the `Task1`-Folder. The package declaration is also `Task1`. – GraphMarv Dec 04 '21 at 16:37
  • You shouldn't use `-cp ./Task1/*:./Correction/Correction.jar`, but `-cp .:./Correction/Correction.jar` – Mark Rotteveel Dec 04 '21 at 17:45
  • Oh, well. That's working, nice. Thank you. – GraphMarv Dec 04 '21 at 21:31

0 Answers0