0

I have a SpringBoot project being build with maven. I have 2 local libraries I am using but only 1 works and I can't figure out why the other doesn't work. I have both jars included as dependencies in my pom.xml file and have verified that the jars are indeed where I expect them to be:

<dependencies>
        <dependency>
            <groupId>com.runner</groupId>
            <artifactId>librunnermax</artifactId>
            <scope>system</scope>
            <version>1.0</version>
            <systemPath>${project.basedir}/../librunnermax/build/lib/librunnermax.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>com.runner</groupId>
            <artifactId>librunnermin</artifactId>
            <scope>system</scope>
            <version>1.0</version>
            <systemPath>${project.basedir}/../librunnermin/build/lib/librunnermin.jar</systemPath>
        </dependency>

    </dependencies>

The relevant code in these libraries is:

Librunnermax.Max

package com.runner.max;

public class Max {
  public static final int MAX = 4;
}

Librunnermin.Min

package com.runner.min;

public class Min {
  public static final int MIN = 0;
}

The spring controller I am using is this:

package com.runner.limits_api;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import com.runner.min.Min;
import com.runner.max.Max;

@RestController
public class LimitController
{
    @GetMapping("/library")
    public int library()
    {
        return Max.MAX;
    }

    @GetMapping("/library2")
    public int library2()
    {
        return Min.MIN;
    }
}

This all builds sucessfully. I get a limits_api.jar file (which does not list the min/max dependencies in the text of the jar). limits_api.jar is copied to my exec/ dir and the dependencies are copied to my lib/ dir.

project/
    src/
        limits_api/
        librunnermax/
        librunnermin/
    lib/
    exec/

When I run it, I can hit the /library endpoint and get the max value. When I hit the /library2 endpoint I get the ClassNotFoundException because Min class cannot be found. No error is thrown when I run the import statements. Why can Max be found but Min cant?

LW001
  • 2,452
  • 6
  • 27
  • 36
Austin
  • 318
  • 1
  • 3
  • 16
  • How are you running it? – g00se Aug 22 '23 at 20:31
  • Why are you setting the scope to `system` and specifying a `systemPath` value? The standard way is to install the jars in your local Maven repo by compiling with `mvn clean install`, and then you don't need to use `system` scope when depending on them. It probably fails because the jar files cannot be found in the right place at runtime. See [Maven Dependency Scopes](https://www.baeldung.com/maven-dependency-scopes). – Jesper Aug 22 '23 at 21:57
  • I believe @g00se is eluding to the issue. When you use systemPath, those jars are not packaged with your application jar. Show us the command line that you're using to start it, you probably just need to ensure that those jars are added to the classpath. – lane.maxwell Aug 22 '23 at 22:00
  • Spring Boot (or rather maven) doesn't include the system scoped dependencies into your jar. This is by design as they are to be added as external dependencies (that is the point of system scoped dependencies). To include add `includeSystemScope` as property to your `spring-boot-maven-plugin` and set it to `true`. See https://stackoverflow.com/a/43822161/2696260 and https://docs.spring.io/spring-boot/docs/current/maven-plugin/reference/htmlsingle/#goals-build-image-no-fork-parameters-details-includeSystemScope – M. Deinum Aug 23 '23 at 07:39

0 Answers0