jar файл не видит файл с конфигурациями config.properties, как решить?
Коллеги, помогите, пожалуйста, решить проблему. в IDE собрал проект, полностью рабочий. через maven - package собираю jar с зависимостями. запускаю и мне прилетает ошибка
java.io.FileNotFoundException: src\main\resources\config.properties (Системе не удается найти указанный путь)
вот где использую данные
public JPanel getMainPanel() {
lableError = new JLabel();
lableError.setVisible(false);
FileInputStream fis;
Properties property = new Properties();
try {
fis = new FileInputStream("src/main/resources/config.properties");
property.load(fis);
urlPR = property.getProperty("urlPR");
urlGZ = property.getProperty("urlGZ");
urlRNF = property.getProperty("urlRNF");
urlSBIC = property.getProperty("urlSBIC");
urlPIS = property.getProperty("urlPIS");
} catch (Exception e) {
lableError.setText("ОШИБКА: Файл свойств properties отсутствует!");
lableError.setVisible(true);
System.err.println("ОШИБКА: Файл свойств properties отсутствует!\n" + e + "\n------------------");
}
return mainPanel;
}
pom.xml
<groupId>org.example</groupId>
<artifactId>UFO-SOAP</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20230618</version>
</dependency>
<dependency>
<groupId>com.sun.xml.messaging.saaj</groupId>
<artifactId>saaj-impl</artifactId>
<version>1.5.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.15</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>org.webjars.bower</groupId>
<artifactId>bytebuffer</artifactId>
<version>5.0.1</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13.3</version>
</dependency>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-api</artifactId>
<version>2.12.4</version>
</dependency>
</dependencies>
я понимаю, что проблема простая. но где она?
Ответы (1 шт):
Скомпилированный исполняемый код находится в папке target. Откройте данную папку, найдите свой класс, в котором выполняется код, и найдите файл config.properties. Вероятнее всего, вы указываете путь, смотря на исходники, а надо смотреть на структуру скомпилированного проекта.
Вообще загружать таким образом ресурсы не очень корректно. Всё, что находится в пакете resources должно попасть в classpath. Это означает, что вы можете получить доступ к файлу более простым способом, описанном по ссылке.
this.getClass().getClassLoader().getResourceAsStream("config.properties");
