Access a directory through a jar

Asked

Viewed 580 times

0

I created a code that uses a file that is located in a given directory (Resources). When I run this code by eclipse, this directory is found and all code actions are done. However, when generating a .jar of this code, it does not find the specified directory. I need to somehow access this directory by the file .jar.

Follow 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 "spin" by .jar error appears:

"src/Resources/Statustesgpes_template.xls (sist. cannot find the path)"

  • You want to access compiled files inside the jar, that’s it?

  • that’s right... "Statustesgpes_template.xls" is one of those files for example

  • You can’t move files inside the jar itself, you’d have to recreate it for that.

  • What do you mean? I don’t understand

  • You are trying to move files inside the jar itself, this is not possible without "repacking" it, ie you would need to create the jar again.

  • I am not trying to move. I read from the "template" file and create a new file

Show 1 more comment

1 answer

1

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

Also, you are using the class File to read internal files in the jar. This class has been made to read files on running operating system file system, to read jar files, you need to access the Classloader and get a stream from it, using ClassLoader#getResourceAsStream(String pathname), because the file only exists in memory, in the context of running the application, and 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 that’s not possible, after all, if the jar is running by JVM, how do you expect to change it? Instead of writing inside the jar, you can copy the file to an external common place in the jar, such as the user folder. See 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();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.