When I call the file with Class # getResource from the jar file, it becomes Not Found and it is a crappy memorandum

Problem event

Place the binary files in the resources directory in a normal Java project.

FileInputStream stream = new FileInputStream(getClass().getResource("/hoge.bin").getPath());

I wrote a program to call and use in such a form. It works fine in the IDE, so when I jar it with mvn and call it from the outside, I get a Not Found Exception.

If you unzip the jar, you'll definitely find hoge.bin directly underneath. Other properties files in the same directory can be read. .. ..

solution

Use Class # getResourceAsStream instead of Class # getResource.

InputStream stream = getClass().getResourceAsStream("/hoge.bin");
```

 Subsequent programs may need to be modified, depending on how the InputStream is used.

 Why?

 Answer here: https://stackoverflow.com/a/941785
 Files that have been jarted cannot be treated as individual files and must be referenced in the form of Stream.
 I'd like to read a little more serious document, but I'm tired of being so shit, so this time.

## Why did you get so shit

 It has a multi-project structure managed by maven, and the entry is project A, but resouces is that of project B, so it took time to identify the problem because I thought it was a mistake in my settings.


Recommended Posts

When I call the file with Class # getResource from the jar file, it becomes Not Found and it is a crappy memorandum
21 Load the script from a file and execute it
Memorandum: When I tried TensorFlow with Tribuo, it didn't work, so I went on a journey to find the head family and lost.
UnsupportedClassVersionError was output when I placed the .war file built with Eclipse on EC2 and executed it.
I want to download a file on the Internet using Ruby and save it locally (with caution)
[Java] Create a jar file with both compressed and uncompressed with the jar command
Create a jar file with the command
When internationalizing is supported by Spring Boot, a specific locale is not translated and I am addicted to it
I tried to make a program that searches for the target class from the process that is overloaded with Java
When I run the source command in the Ubuntu Makefile, it says "source: command not found"
What to do when is invalid because it does not start with a'-'
File path is not recognized when executing an external command from a Java application
At the time of dialogReturn event, I checked because it is not updated even if I specify a component with update
What is the difference between a class and a struct? ?? ??
Write a class in Kotlin and call it in Java
I faced a problem that JS is not read after page transition and JS is read when loaded.
When I run docker-compose exec in crontab, I get "the input device is not a TTY"
How to read a library from a JAR file with VS Code << How to not use Maven / Gradle >>
When a Java file created with the Atom editor is garbled when executed at the command prompt
Find the address class and address type from the IP address with Java
I want to call a method and count the number
About the behavior when doing a file map with java
I tried to express the result of before and after of Date class with a number line
Convert Excel to Blob with java, save it, read it from DB and output it as a file!
If you get a "Quantum Renderer: no suitable pipeline found" error when running the jar file