3

I am using Java8 with Spring running on a Wildfly server.

I have the following package:

enter image description here

In LanguageChunkerServiceImpl, I am trying to get a handle on en-parser-chunking.bin, but I get a error:

java.io.FileNotFoundException: en-parser-chunking.bin (The system cannot find the file specified)

My code:

LanguageChunkerServiceImpl.java

new FileInputStream("en-parser-chunking.bin");

or

new FileInputStream("./src/main/java/com/jobs/spring/service/lang/en-parser-chunking.bin");

When I run this from the main method, the following does work though:

new FileInputStream("./src/main/java/com/jobs/spring/service/lang/en-parser-chunking.bin");

Can anyone please advise what the path should be?

Thank you

Richard
  • 8,193
  • 28
  • 107
  • 228
  • http://stackoverflow.com/questions/4871051/getting-the-current-working-directory-in-java Find your root path and navigate to it. – Compass Nov 15 '16 at 16:30
  • You probably want to take a look at [Class#getResourceAsStream](https://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResourceAsStream%28java.lang.String%29). – Mena Nov 15 '16 at 16:30
  • [this](http://stackoverflow.com/questions/2914375/getting-file-path-in-java) can help you – Ray Lloy Nov 15 '16 at 16:32
  • Thanks. I tried this, `ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); URL resource = classLoader.getResource("en-parser-chunking.bin");` but `resource` is `null` – Richard Nov 15 '16 at 16:42
  • I would rather not get the absolute path of the current location, and then deprive the required files location from there. I think this may not work when I deploy to other servers. – Richard Nov 15 '16 at 16:53
  • @mena thanks, I am not sure if I'm using it correctly, but the following returns `null`. `ClassLoader.getSystemResourceAsStream("en-parser-chunking.bin")` – Richard Nov 15 '16 at 17:08
  • The path of your resource is `com/jobs/spring/service/lang/en-parser-chunking.bin`, not `en-parser-chunking.bin`. And it's not a system resource. Just a plain resource. – JB Nizet Nov 15 '16 at 17:15
  • Use **Class**.getResourceAsStream as Mena suggested, not ClassLoader.getResourceAsStream. – VGR Nov 15 '16 at 17:27

2 Answers2

1

You should put the file in resource folder not in src/java, if your using spring.

srikanth
  • 120
  • 1
  • 9
0

The following works:

If you are using Spring, put the file in the resources dir.

        ClassLoader classLoader = getClass().getClassLoader();
        File file = new File(classLoader.getResource("en-parser-chunking.bin").getFile());
        System.out.println(file.getAbsolutePath());
        modelInParse = new FileInputStream(file.getAbsolutePath());
Richard
  • 8,193
  • 28
  • 107
  • 228