1

I put a file to be read into my resources folder

src
|_main
   |_resources
     |_graphqls
       |_test.graphqls

The following snippet reads the file

final String pathToSchemaFile = this.getClass().getClassLoader().getResource("graphqls/test.graphqls").getFile();
final File = new File(pathToSchemaFile);

this is the result I get when I evaluate the File object returned by .getFile() from the preceding snippet.

file:\C:\maven_repository\com\...\app.jar!\graphqls\test.graphqls

enter image description here

When running the following code new FileReader(file) this exception is being thrown

Method threw 'java.io.FileNotFoundException' exception.
file:\C:\maven_repository\com\...\app.jar!\graphqls\test.graphqls (The filename, directory name, or volume label syntax is incorrect)
java.io.FileNotFoundException: file:\C:\maven_repository\com\...\app.jar.jar!\graphqls\test.graphqls (The filename, directory name, or volume label syntax is incorrect)
6
  • 1
    Why do you need the file, it looks like you should just use getResourceAsStream. Commented May 9, 2018 at 8:37
  • The GraphQL Framework expects a file that represents the graphQL schema file. Streaming it content would be my second option but I'd like to just put the file reference Commented May 9, 2018 at 8:38
  • Your example shows a FileReader which is an InputStreamReader, can you show the api where you need an actual file? Commented May 9, 2018 at 8:41
  • It is this one here: github.com/graphql-java/graphql-java/blob/master/src/main/java/… see line 40 Commented May 9, 2018 at 8:45
  • The second constructor uses a Reader so you can use the input stream. Commented May 9, 2018 at 8:47

2 Answers 2

2

You're accessing a file that is actually inside a JAR file (like a ZIP).

If your jar is on the classpath:

InputStream is = YourClass.class.getResourceAsStream("1.txt");

If it is not on the classpath, then you can access it via:

URL url = new URL("jar:file:/absolute/location/of/yourJar.jar!/1.txt");
InputStream is = url.openStream();
Sign up to request clarification or add additional context in comments.

3 Comments

I know I am accessing a file that is inside a jar. But what is wrong with this approach? Why is FileReader crying like a little girl?
I'm not 100% sure, but as far as I know the File was used to represent something on the filesystem. And the file within the jar is not handled like any ordinary file on the file system.
Hm okay that might explain things if this is a true statement
0

You can avoid the file by creating an InputStreamReader.

InputStreamReader reader = new InputStreamReader(
    this.getClass().getResourceAsStream("/graphqls/test.graphqls"), 
    StandardCharsets.UTF_8
);

It is advised to use YourClassName.class.getResourceAsStream() unless you have some specific reason to use the .getClass && .getClassLoader.

4 Comments

I actually have to use .getClassLoader() because it won't find the file otherwise.
You're using the relative pathname. So if your class is in a package, then then when it looks it up, it will look relative to the package you're in.
To be honest I do not have a lot of experience using Files and the specific filepath conventions. Is the way I do it in any way problematic? If yes - what would you recommend me to do?
@xetra11 It's good that you're using a resource instead of just using a file. The rest are detail, that are good practice but not necessary. here is a pretty concise but thorough article.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.