Skip to content

Java application can't read file from Resources #158

@BTony95

Description

@BTony95

My actions before raising this issue

Expected Behaviour

I am currently experimenting with Openfaas and Java. I made a simple function to check the body of an HTTP call against a JSON schema.This JSON schema is read from the resources folder.

Current Behaviour

Once deployed on the openfaas environment in my kubernetes cluster it fails to read the file
I get an error of the following type:

2019/07/05 12:40:13 stdout: 2019-07-05 12:40:13 ERROR Utils - Exception:                                                    
webhook-xxxxxxxxx-xxxxx:webhook java.io.FileNotFoundException: file:/home/app/entrypoint-1.0/lib/function-1.0.jar!/InputSchema.json (No such file or directory)                                                                                

Context

The flow of my program:

  1. Openfaas receives an HTTP call with JSON body
  2. Load Input schema from Resources
  3. Check the body against the schema
  4. Returns status code 200 if correct or 400 if wrong

Your Environment

  • FaaS-CLI version: 0.8.19

  • Docker version: 18.09.6

  • Are you using Docker Swarm or Kubernetes (FaaS-netes)? Kubernetes

  • Operating System and version (e.g. Linux, Windows, MacOS): Linux

  • Code example or link to GitHub repo or gist to reproduce problem:

public static String readResource(String fileName) {

        StringBuilder result = new StringBuilder();

        //Get file from resources folder
        ClassLoader classLoader = Utils.class.getClassLoader();
        File file = new File(classLoader.getResource(fileName).getFile());

        try (Scanner scanner = new Scanner(file)) {

            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                result.append(line).append("\n");
            }
            System.out.println(result);
            scanner.close();

        } catch (IOException ex) {
            log.error("Exception: ", ex);
        }
        return result.toString();

    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions