I'm trying to read a csv file, everything works before is deployed on jboss and I don't know how to solve it: my loader is this:
public Map<String, String> getScript() throws IOException {
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("scriptSQL.csv").getPath());
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
Map<String, String> map = new HashMap<>();
while ((line = br.readLine()) != null) {
String arr[] = line.split(";");
map.put(arr[0], arr[1]);
}
System.out.println(map);
this.sqlScript = map;
return map;
}
public String getKey(String key) {
if (this.sqlScript != null && this.sqlScript.get(key) != null && !this.sqlScript.get(key).isEmpty()) {
return sqlScript.get(key);
} else {
try {
sqlScript = getScript();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return sqlScript.get(key);
}
The error when is deployed is this:
Caused by: java.io.FileNotFoundException: C:\Program Files\wildfly-18.0.0.Final\standalone\deployments\archiconEar.ear\archiconWeb.war\WEB-INF\classes\scriptSQL.csv (Impossible find the path)
how can I say to look in the resource folder and not in this one or anything who could help me please
CodePudding user response:
Use full path to locate your file. Something like below :
File file = new File(classLoader.getResource(PROJECT_PATH "path\to\your\file\scriptSQL.csv").getPath());
Set PROJECT_PATH using either properties file or system variable. E.g.
File file = new File(classLoader.getResource(System.getenv("PROJECT_PATH") "path\to\your\file\scriptSQL.csv").getPath());
This value will be different when you run application directly in local and when you deploy it to run in any server
CodePudding user response:
Better not to mix up your resources with your normal code packages. This uses the csv resource as placed at the root. You'd be better off creating a 'resources' directory for easier demarcation though:
public Map<String, String> getScript() throws IOException {
Map<String, String> map = new HashMap<>();
try (BufferedReader br = new BufferedReader(
new InputStreamReader(getClass().getResourceAsStream("/scriptSQL.csv")))) {
String line = null;
while ((line = br.readLine()) != null) {
String arr[] = line.split(";");
map.put(arr[0], arr[1]);
}
}
System.out.println(map);
this.sqlScript = map;
return map;
}