Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java application can't read file from Resources #158

Open
3 tasks done
BTony95 opened this issue Jul 5, 2019 · 4 comments
Open
3 tasks done

Java application can't read file from Resources #158

BTony95 opened this issue Jul 5, 2019 · 4 comments

Comments

@BTony95
Copy link

BTony95 commented Jul 5, 2019

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();

    }
@alexellis alexellis transferred this issue from openfaas/faas Jul 5, 2019
@alexellis
Copy link
Member

I'm moving this issue to the templates repo where the Java code exists.

@alexellis
Copy link
Member

See also: #86 by @ivanayov

@ieugen
Copy link

ieugen commented Jul 23, 2020

@BTony95 : can you check if the resource is in the jar file ?
kubectl exec into the container, unzip and check the file.

Also this might help: https://www.baeldung.com/java-classpath-resource-cannot-be-opened .

try (InputStream inputStream = getClass().getResourceAsStream("/input.txt");
	    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
	    String contents = reader.lines()
	      .collect(Collectors.joining(System.lineSeparator()));
}

@ybasapur
Copy link

@BTony95 : can you check if the resource is in the jar file ?
kubectl exec into the container, unzip and check the file.

Also this might help: https://www.baeldung.com/java-classpath-resource-cannot-be-opened .

try (InputStream inputStream = getClass().getResourceAsStream("/input.txt");
	    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
	    String contents = reader.lines()
	      .collect(Collectors.joining(System.lineSeparator()));
}

best solution I come across, after struggling for many hours to make it work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants