How to copy a file inside the jar to a folder outside it?

1

I put images inside a package in the project. I need at run time to display this image in an external program (in the notify-send case of linux), this program needs the exact path of the image, so I had the idea of copying the image into the temporary folder of the system, an absolute path and the external program can access the image.

The idea worked out at first when running the program by Netbeans it works as desired, but when creating the .jar file and do the same test the system "bonnet".

The code is this

public class Main {

    public File getImagem(String fileName) throws URISyntaxException {
        String arquivo = "/pacote/imagem/" + fileName;
        //o erro acontece nessa linha abaixo, ao chamar o método toURI
        return new File(getClass().getResource(arquivo).toURI());

    }

    public static void main(String[] args) throws IOException, URISyntaxException {
        String fileName = "13.jpeg";
        //caminho da imagem dentro da pasta tmp do sistema
        String imagem = System.getProperty("java.io.tmpdir") + "/" + fileName;

        //file do arquivo que será copiado para a pasta tmp do sistema
        File tmp = new File(imagem);

        //File da imagem dentro do jar
        File src = new Main().getImagem(fileName);

        //copia arquivo de dentro do jar para a pasta temporaria
        copyFile(src, tmp);

        //exibe notificação mostrando a imagem que está dentro da pasta tmp do sistema
        exibirNotificacao(imagem, "titulo", "conteudo");
    }
    public static copyFile(File src, File destino){
         //...
    }    

    public static void exibirNotificacao(String url_imagem, String titulo, String conteudo) throws IOException {
         //...
    }
}

The exception message that occurs when executing the jar is

$ ./dist/Notify.jar 
Exception in thread "main" java.lang.IllegalArgumentException: URI is not hierarchical
    at java.io.File.<init>(File.java:418)
    at pacote.Main.getImagem(Main.java:30)
    at pacote.Main.main(Main.java:43)

Solution

public InputStream getImagem(String fileName) throws URISyntaxException {
        String arquivo = "/pacote/imagem/" + fileName;
        return getClass().getResourceAsStream(arquivo);

    }

    public static void main(String[] args) throws IOException, URISyntaxException {
        String fileName = "13.jpeg";
        //caminho da imagem dentro da pasta tmp do sistema
        String destino_imagem = System.getProperty("java.io.tmpdir") + "/" + fileName;


        InputStream inputImagem = new Main().getImagem(fileName);
        OutputStream outputImagem = new FileOutputStream(destino_imagem);

        byte[] buffer = new byte[inputImagem.available()];
        inputImagem.read(buffer);
        outputImagem.write(buffer);

        if(inputImagem != null)
            inputImagem.close();
        if(outputImagem!=null)
            outputImagem.close();


        //exibe notificação mostrando a imagem que está dentro da pasta tmp do sistema
        exibirNotificacao(destino_imagem, "titulo", "conteudo");
    }

I had to deal with the exceptions, but it's up to everyone, that's all. Thanks to @bruno

    
asked by anonymous 03.05.2015 / 21:05

1 answer

2

In your getImage method you should use: getResourceAsStream()

InputStream input =  getClass().getResourceAsStream("/pacote/imagem/" + fileName);

When the resource (image, text file, ..) is "compressed / grouped" as a jar or any other type of package you should use getResourceAsStream .

This is because the jar is a type of file, just like the zip, which groups several files together, but for the operating system it is as if it were a single file. To access a specific part you need to process the file as a stream.

Link to the method

    
03.05.2015 / 21:16