Access a directory by a jar

0

I've created a code that uses a file that is located in a certain directory (resources). When I squeeze this code through eclipse, this directory is found and all code actions are done. However, by generating a .jar of this code, it does not find the specified directory. I need some way to access this directory through the .jar file.

Here's part of my code:

teste = Json.criarJson(teste, json);

DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
Date date = new Date();

CopyTemplate.copyfile("src/resources/StatusTestesGPES_template.xls", "src/resources/StatusTestesGPES_"+ dateFormat.format(date)+".xls", teste);

Copyfile:

public static void copyfile(String ORIcaminho, String DEScaminho, Test[] teste) throws InvalidFormatException, ParseException
{
      try
      {
          File origem = new File(ORIcaminho);
          File destino = new File(DEScaminho);
          InputStream in = new FileInputStream(origem);

          //For Overwrite the file.
          OutputStream out = new FileOutputStream(destino);

          byte[] buf = new byte[1024];
          int len;
          while ((len = in.read(buf)) > 0)
          {
              out.write(buf, 0, len);
          }

          in.close();
          out.close();
      }
      catch(FileNotFoundException e)
      {
          JOptionPane.showMessageDialog(null, e.getMessage() , "Erro", JOptionPane.ERROR_MESSAGE);
          throw new RuntimeException(e);
      }
      catch(IOException e)
      {
          JOptionPane.showMessageDialog(null, e.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
          throw new RuntimeException(e);
      }
}

When I "squeeze" through .jar the error appears:

  

"src / resources / StatusTestesGPES_template.xls (The system can not find the path)"

    
asked by anonymous 07.11.2017 / 18:33

1 answer

1

The reason for the error is probably that you are reporting a path only recognized by eclipse because the src folder is created only in the project to store the .java package and frame structure created within the IDE.

In addition, you are using the File class to read internal files to the jar. This class was made to read files in the operating system filesystem, to read jar files, you need to access the ClassLoader and get it from a stream, using ClassLoader#getResourceAsStream(String pathname) , because the file only exists in memory, in the application execution context, not as an operating system file.

Another problem I noticed in your code is that you seem to want to copy files inside the running jar itself, and this is not possible, after all, if the jar is running by the JVM, how do you expect to change it? Instead of writing inside the jar, you can copy the file to a common place outside the jar, such as the user folder. Here's a basic example:

//cria um stream do arquivo dentro do jar para manipulação
InputStream in = ClassLoader.class.getResourceAsStream("/com/example/res/textfile.txt");
//caminho do novo arquivo a ser criado na pasta de usuario do sistema em execução
String externalPath = System.getProperty("user.home") + File.separator + "externaltxt.txt";
//efetua a cópia do arquivo interno do jar para fora dele
Files.copy(in, Paths.get(externalPath), StandardCopyOption.REPLACE_EXISTING);
in.close();
    
08.11.2017 / 15:21