0

I've a jar library that needs to be read as an argument to my class MyProject and a respective app property in application.properties my.customJar=/WEB-INF/lib/myJar.jar to use the jar in my local app run and it works well locally but it fail on the pcf cloud. I did some changes & respective to this property, I've tried adding an override in cloud environment application-dev.properties i.e. my.customJar=${project.basedir}/src/main/webapp/WEB-INF/lib/myJar.jar I've also tried with relative path my.customJar=./src/main/webapp/WEB-INF/lib/myJar.jar but neither of them appear to have worked. The problem arises specifically when I send this code to the pcf cloud I am getting FileNotFoundException. And it fails there with error mentioned below. Can anyone please guide me with how can I set the path for the other profiles. Also, please let me know if there is any more convenient way to do this as well?

Note: I am using multiple spring profiles.

Error I am getting

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myController' defined in file [/home/abc/app/BOOT-INF/classes/com/example/myController.class]: Unsatisfied dependency expressed through constructor parameter 2; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jarUserBean' defined in class path resource [com/example/configuration/CommonConfigs.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.myJar.api.MyProject]: Factory method 'jarUserBean' threw exception; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/lib/myJar.jar]

2021-08-27T07:53:53.647+01:00 [APP/PROC/WEB/0] [OUT]

Here, the jarUserBean is a bean created using the jar's insputStream i.e.

@Value("${my.customJar}")
private String pathToJar;

@Bean
@ConditionalOnProperty(prefix = "my", name = "customJar")
public MyProject jarUserBean() throws IOException, ProjectInvalidException, InterruptedException {
    Resource resource = loader.getResource(pathToJar);
    InputStream is = resource.getInputStream();
    return new MyProject(is);
}
Jack
  • 29
  • 6
  • You are missing that myJar.jar in web-inf folder. Check if that is there or not. Check your build process too on how to include that jar. – Ajay Kumar Aug 27 '21 at 15:55
  • yes @AjayKumar it's their and for checking I made a copy of the same jar in my src/main/resources folder as well.. it's still failing just on the devOps machine but runs nicely on my local using local spring profile.. – Jack Aug 28 '21 at 06:25
  • Hmm.. take a look at this - https://stackoverflow.com/questions/39130707/unsatisfied-dependency-expressed-through-constructor-parameter-0-no-qualifying this si the exact error you are getting – Ajay Kumar Aug 28 '21 at 12:45
  • @AjayKumar - it's not failing in my local, I can see the beans created successfully in my local environment and I can call the APIs as well in my local environment. so, I am sure, it's not a bean configuration issue.. it's the problem with setting up the path for the jar in cloud profile 'dev' – Jack Aug 29 '21 at 12:13
  • Its a long shot but try making “String pathToJar” to public instead of private and share the outcome. Follow the steps here - https://www.baeldung.com/spring-inject-static-field – Ajay Kumar Aug 29 '21 at 13:44
  • why do you think it would say FileNotFound, if it can't see the path? it should say something like InvalidArgument or Path not specified.. – Jack Aug 31 '21 at 02:11

1 Answers1

0

Adding this plugin in the pom helped me reading it from the classpath itself.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
        <configuration>
          <nonFilteredFileExtensions>
                <nonFilteredFileExtension>jar</nonFilteredFileExtension>
          </nonFilteredFileExtensions>
        </configuration>
Jack
  • 29
  • 6