How to write resource paths in Java? Good day to all. I'm currently learning java. I am developing in intellij idea. Undertook to implement a simple directory using javafx. DB sqlite, to connect maven pulls the necessary libraries. Everything works as long as you run from idea and stop working after building in the jar. In the process of studying, I realized that I had collected the wrong things and had no confusion with my ways. Therefore, the question is how to correctly set paths to the resources folder? In idea, when you create a maven project, we have the following structure

src --main ----java ------Main ----resources ------db --------test.db ------test.fxml --test pom.xml 

How to set the path to test.fxml and test.db in Main? At the moment, fxml (moved it to the root to the Main) get through

  Parent root = FXMLLoader.load(getClass().getResource("main.fxml")); 

But for me it is not so. Why when calling

 System.out.println(getClass().getClassLoader().getResource("test.db")); 

from Main In the terminal issues

 file:/E:/JavaProjects/test/target/classes/test.db <?xml version="1.0" encoding="UTF-8"?> 

http://maven.apache.org/xsd/maven-4.0.0.xsd "> 4.0.0 UTF-8 maven maven 1.0

 <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> <configuration> <archive> <manifestFile>${project.basedir}/src/main/java/META-INF/MANIFEST.MF</manifestFile> </archive> </configuration> </plugin> </plugins> <resources> <resource> <directory>src/main/java/ru/varizo/maventest/resources</directory> <includes> <include>**/*.txt</include> </includes> </resource> </resources> </build> 

  • Your file is in the db folder. so you need to write getResource("db\test.dbl") - Senior Pomidor

1 answer 1

Your file is in the db folder. therefore, you must specify the file location inside the resource

 URL resource = Main.class.getClassLoader().getResource("db/file.db"); File f = new File(resource.toURI()); System.out.println(f.exists()); 

true result

  • Checked Works. But the question arose - if I understood correctly after packing all my stuff in the jar I will not be able to open the db file. Yes, and he is not needed there. It can be put nearby. In this case, how to contact him? - Varfalamey Isoldin
  • 1) you can add to jar. 2) add to classPath and you can access - Senior Pomidor
  • A simple example. There is Main next to it is txt / info.txt.FileReader file = new FileReader (new File (Main.class.getClassLoader (). GetResource ("txt / info.txt"). GetPath ())); BufferedReader in = new BufferedReader (file); fulfills. But it is necessary to collect all this in a jar that gives the following error Exception in thread "main" java.io.FileNotFoundException: file: \ E: ****** \ target \ maven-1.0.jar! \ Txt \ info.txt ( Syntax error in the file name, folder name or volume label) - Varfalamey Izoldin
  • show your pom -
  • Posted at the beginning - Varfalamey Isoldin