Run java jar, does not load project files

1

I have a project in java, in which I open a pdf file with an option, when I run it from IntelliJ, it uploads the file without problems, but at the time of creating the .jar it does not load it and it tells me that it does not exist the file. The code is as follows:

if (e.getSource() == menuLenguaje){
        try {
            File path = new File(getClass().getResource("/Archivos/lenguaje.pdf").getFile());
            Desktop.getDesktop().open(path);
        }catch (IOException ex) {
            ex.printStackTrace();
        }

I know if I put it in a folder (package) called Files or I send it to call from the location in src, it does not load the file.

    
asked by IsraelM17 27.05.2018 в 04:37
source

1 answer

0

Doing some tests here, it has worked for me creating a temporary file of a PDF, that is, copying it and using getResourceAsStream() in the following way:

try {
  Path temp = Files.createTempFile( "temporal", ".pdf" );
  temp.toFile().deleteOnExit();

  // Sin backslash por delante en la ruta del recurso...
  InputStream IS = MiClase.class.getResourceAsStream( "Archivos/lenguaje.pdf" );
  Files.copy( IS, temp, StandardCopyOptions.REPLACE_EXISTING );
  Desktop.getDesktop().open( temp.toFile() );
} catch( Exception e) {
  e.printStackTrace();
}

He worked perfectly from my development environment as well as from the JAR ready for distribution.

Edit:

Something not minor; if somehow one wonders in what directory would be the temporary file if it is not deleted with the deleteOnExit() , with a simple System.out.println(temp) the entire route is seen.

    
answered by 27.05.2018 / 22:04
source