diff --git a/main/src/main/java/sd/config/SimulationConfig.java b/main/src/main/java/sd/config/SimulationConfig.java index ce6265a..4a3fe89 100644 --- a/main/src/main/java/sd/config/SimulationConfig.java +++ b/main/src/main/java/sd/config/SimulationConfig.java @@ -27,9 +27,43 @@ public class SimulationConfig { */ public SimulationConfig(String filePath) throws IOException { properties = new Properties(); - try (InputStream input = new FileInputStream(filePath)) { - properties.load(input); + /**Tenta carregar diretamente a partir do sistema de ficheiros, se o ficheiro não existir + * (por exemplo quando executado a partir do classpath/jar), + * faz fallback para carregar a partir do classpath usando o ClassLoader. + */ + IOException lastException = null; + + try { + try (InputStream input = new FileInputStream(filePath)) { + properties.load(input); + return; // carregado com sucesso a partir do caminho fornecido + } + } catch (IOException e) { + lastException = e; + //tenta carregar a partir do classpath sem prefixos comuns + String resourcePath = filePath; + //Remove prefixos que apontam para src/main/resources quando presentes + resourcePath = resourcePath.replace("src/main/resources/", "").replace("src\\main\\resources\\", ""); + //Remove prefixo classpath: se fornecido + if (resourcePath.startsWith("classpath:")) { + resourcePath = resourcePath.substring("classpath:".length()); + if (resourcePath.startsWith("/")) resourcePath = resourcePath.substring(1); + } + + InputStream resourceStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourcePath); + if (resourceStream == null) { + //como último recurso, tentar com um leading slash + resourceStream = SimulationConfig.class.getResourceAsStream('/' + resourcePath); + } + + if (resourceStream != null) { + try (InputStream input = resourceStream) { + properties.load(input); + return; + } + } } + if (lastException != null) throw lastException; } // --- Network configurations ---