(I am new to java, please be understanding)
I am writting a application in java that needs a config file, the config file is a json file, I neeed to open it and read it's contents but no matter how I try I cannot do it, I have done hours of looking on the internet but I cannot find a working way
I have tried using my POM.xml to create a resource folder, and then using .getResource() but that doesn't seem to work
My java
public class MusicOrganizer {
public static String configURL = "assets/config.json";
public static String configSource;
public static void main(String[] args) {
try {
configSource = MO.getFileFromResources(configURL);
} catch (IOException e) {
// This isn't what is going wrong
System.out.println("Error reading config file!\n" + e + "\nDisplaying error screen...");
errorScreen.main("Error Reading Config", "Try redownloading the application.","ERC_001");
return;
}
}
private String getFileFromResources(String fileName) throws IOException {
ClassLoader classLoader = getClass().getClassLoader();
URL resource = classLoader.getResource(fileName);
if (resource == null) {
throw new IllegalArgumentException("file is not found!"); // this is what is going wrong
} else {
return printFile(new File(resource.getFile()));
}
}
private static String printFile(File file) throws IOException {
if (file == null) throw new IllegalArgumentException("file is not found!");
String source = "";
try (FileReader reader = new FileReader(file);
BufferedReader br = new BufferedReader(reader)) {
String line;
while ((line = br.readLine()) != null) {
source += line;
}
return source;
}
}
}
My POM.xml where I create my "resource folder"
<build>
<resources>
<resource>
<directory>assets</directory>
</resource>
</resources>
</build>
Please tell me what I am doing wrong, I want to have my java file (while in compressed .jar form) read what is in the "config.json" file.
I am using NetBeans 11.1, java version "12.0.2" 2019-07-16, Java(TM) SE Runtime Environment (build 12.0.2+10), Java HotSpot(TM) 64-Bit Server VM (build 12.0.2+10, mixed mode, sharing), and a MacBook Pro
My directory paths:
>ROOT
>nb-configuration.xml
>nbactions.xml
>pom.xml
>src
>main
>java
>com
>[my name]
>musicorganizer
[java files]
>JSON
[java files]
>resources
config.json
>test
>java
[nothing here]
>target
[Stuff]