简体   繁体   中英

Jar that read file

I have a code that deals with elasticsearch index. One of its steps, the program needs to read a jsonschema file in order to continue its execution. The code works well on my machine, but when I execute it as jar file inside a docker container, it gives me the following error:

java.io.FileNotFoundException: dm.jsonschema (No such file or directory)

the code that loads the schema is:

public class loadSchema {

private static final String JSON_SCHEMA_DOCUMENT = "dm.jsonschema";
....
public static JsonSchema tryLoadJSONSchema() {
    JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
    JsonNode cdmSchema = null;
    try {
        cdmSchema = JsonLoader.fromPath(JSON_SCHEMA_DOCUMENT);
    } catch (IOException e) {
        System.out.println(e);
        System.exit(-1);
    }

I placed the jsonschema file next to the jar file in the container, but it keeps giving me the same error. Any idea how to solve this problem?

The relative path of the File is resolved from wherever you are starting the java executable (mind the user.home ), not from where the jar archive was located.

The best approach would be externalising this file location using a system property. For example if you define a jsonSchemaPath property:

String path = Sytem.getProperty("jsonSchemaPath");
JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
JsonNode cdmSchema = JsonLoader.fromPath(path);

Then you can change it in the java command:

java -jar yourcode.jar -DjsonSchemaPath=/path/to/dm.jsonschema

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM