Bankelaal
Bankelaal

Reputation: 418

Problem with loading a third party jar inside a spring boot application

I am creating a Spring Boot application, for a business reason I need to import a 3rd party jar which also reads some config parameter from a file named application.properties.

I have defined all the configs required by my app and the 3rd party jar in the spring boot application.properties file.

When I run this application from Eclipse all is well, but when I run it from outside by using command the code inside the jar can't find the application.properties file and throws exception;

java -jar myapp.jar

when I change it to following command and by placing the application.properties file outside the jar it works;

java -Xbootclasspath/a: -jar myapp.jar

How can make it work without placing the application.properties file from outside the jar?

Some additional Info; 3rd party jar is a lib jar included as my maven dependency. Inside the 3rd Party code there is a place where it's loading application.properties file, but throws exception.

    Properties cfg = new Properties();
    FileInputStream is = null;

    URL url = DemoApplication.class.getResource("/");
    if (null != url) {

        String configPath = url.getFile()+ "application.properties";
        try {
            is = new FileInputStream(configPath);
            cfg.load(is); // It fails here
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

Exception:

file:/D:/Temp/target/myapp.jar!/BOOT-INF/classes!/application.properties FAILED load config.java.io.FileNotFoundException: file:\D:\Temp\target\myapp.jar!\BOOT-INF\classes!\application.properties (The filename, directory name, or volume label syntax is incorrect) 

Upvotes: 0

Views: 1041

Answers (3)

GeertPt
GeertPt

Reputation: 17854

That 3rd Party jar uses a FileInputStream with a URL that starts with something derived from Class.getResource(...).

That's never going to work with jar files.

What can work is to skip the FileInputStream, but use Class.getResourceAsStream(...):

InputStream is = DemoApplication.class.getResourceAsStream("/application.properties")

Upvotes: 0

Daniel
Daniel

Reputation: 1037

If your thirdparty-app is a maven dependency of myApp you can load it from the classpath:

Properties cfg = new Properties();
cfg.load(DemoApplication.class.getClassLoader().getResourceAsStream("application.properties"));

Upvotes: 0

Hesham Osman
Hesham Osman

Reputation: 111

If it's a maven project, try to use (mvn package) command and then run the jar generated under the target folder

Upvotes: 0

Related Questions