5
I created a Java project with the following structure:
src
|-br.foo.settings
| |- settings.json
| |- Settings.java
|
|- ... outros pacotes ...
As I am used to the JSON file format, I thought of creating one to define the main settings of the application (as you can see, it is in the package br.foo.settings
).
To illustrate, there is some information about repositories, dependencies, directories where I should save application data, etc. An example of structure:
{
locais: [
{"padrao":"stackoverflow"},
{"usuario":"2"},
...
],
...
}
The class Settings
is responsible for obtaining a configuration value based on key. For example, suppose I want the value of key "standard" (in "sites") I would call a
method that returns the "stackoverflow".
The problem...
All of this works normally when I run tests and run the project in the IDE, but when I do build and try to run the . created jar is launched an exception of NullPointerException
for not finding the file settings.json
.
Here’s my first question:
This file is not packaged along with the . jar?
Well, continuing, as the file is in the same class package Settings
, I am using the following code to get the file:
// Settings.java
File arquivoDeConfiguracao = FileUtils.toFile(getClass().getResource("settings.json"));
The use of "getClass().getResource(...)
" it’s always worked for me, but in this case it’s frustrating me!
Is there any way to package this . json file along with . jar? I’m pretty sure the problem is this, because the method I’m using to get the file is picking up a null object, but the funny thing is that when I run by the IDE it works OK.
My second question is:
I am using Maven and noticed that it has a directory called "Resources". If I move this JSON file there, it will be packaged along with . jar?
I accept alternative suggestions, for now I am considering this to read the JSON file the best. For example, some other way to write a file with data that should be used by the application that is not creating enum
?
To answer your first question you can open the
.jar
with some application that opens zip files and see if the file is inside. If it is inside, probably the problem is in the parameter ofgetResource()
which would have to be the file path based on the root of the jar.– GabrielOshiro
@Gabrieloshiro I’ll check, I hadn’t thought about it.
– Renan Gomes
Your second question is easier, "Yes!". If you are using Maven it is always advisable to use their directory structure, since Maven was developed under the concept "Convention on configuration". The directory
res
will bundled next to the jar unless you specify that you do not want to pack them.– GabrielOshiro
@Gabrieloshiro I extracted the files from the . jar and the JSON file is there, following the same structure as in the question. I don’t know what the problem is, I’m almost starting the project from scratch because I haven’t been able to solve it for weeks.
– Renan Gomes
Which way do you pass to
getResource()
? Try to pass the path of your jar by swapping the points of the Packages for barssrc/br/foo/settings/settings.json
– GabrielOshiro
Thanks for the tips @Gabrieloshiro, I managed to solve! : D
– Renan Gomes