Hi guys.

I'm working on a christmas present for my dad, it's a program that installs 8 Black Keys songs on his computer. I'm having some trouble with it though. I can't figure out how to get the files out of the jar file. I used to access them with \\src\\package\\filename.fileending and had no problem but it's not working for some reason now. I'm using the java.nio package to copy the files, and this is my code. (I got the ExportResource method from this StackOverflow question)

The Window Class

package installer;

import javax.swing.*;

import java.awt.*;

public class InstallerWindow extends JFrame {

    JPanel panel;
    JTextField filePath;
    JLabel filePathValidator;
    JPanel panel1;
    JPanel panel2;
    JButton install;
    JButton next;
    CardLayout cl;
    JLabel label1 = new JLabel("Choose what folder to put the music in");

    public InstallerWindow(Engine engine){

        cl = new CardLayout();
        panel = new JPanel();
        filePath = new JTextField("C:\\Users\\Aguilera\\Music");
        install = new JButton("Install Music");
        next = new JButton("Next");
        filePathValidator = new JLabel("Valid");
        panel1 = new JPanel();
        panel2 = new JPanel();

        panel1.setLayout(new BoxLayout(panel1, BoxLayout.PAGE_AXIS));
        panel1.add(new JLabel("Happy Birthday Pops!"));
        panel1.add(new JLabel("This program will install some Black Keys music."));

        panel2.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        panel2.add(label1 ,c);
        c.gridy = 1;
        c.ipadx = 70;
        panel2.add(filePath, c);
        c.gridx = 1;
        c.ipadx = 0;
        panel2.add(filePathValidator, c);
        c.gridy = 2;
        c.gridx = 0;
        panel2.add(install, c);

        panel.add(panel1, "InfoPanel");
        panel.add(panel2, "InstallPanel");





Logical Class

package installer;

import javax.swing.*;

import java.awt.event.*;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import static java.nio.file.StandardCopyOption.*;

public class Engine implements ActionListener {

    InstallerWindow installer;

    public Engine(){

        installer = new InstallerWindow(this);
        installer.setLocation(300, 300);


    public static void main(String args[]){

        new Engine();


    public void actionPerformed(ActionEvent e) {



        }else if(e.getActionCommand().equals("install")){



            System.out.println("There was a glitch. Oops");



    public void installMusic() {


            Path target = Paths.get(installer.filePath.getText());


            installer.label1.setText("Installing file 1");
            Files.copy(Paths.get(ExportResource("/[Black Keys] Fever.mp3")), target, REPLACE_EXISTING);
            installer.label1.setText("Installing file 2");
            Files.copy(Paths.get(ExportResource("/[Black Keys] Gold On The Ceiling.mp3")), target, REPLACE_EXISTING);
            installer.label1.setText("Installing file 3");
            Files.copy(Paths.get(ExportResource("/[Black Keys] Gotta Get Away.mp3")), target, REPLACE_EXISTING);
            installer.label1.setText("Installing file 4");
            Files.copy(Paths.get(ExportResource("/[Black Keys] Howlin For You.mp3")), target, REPLACE_EXISTING);
            installer.label1.setText("Installing file 5");
            Files.copy(Paths.get(ExportResource("/[Black Keys] Little Black Submarines.mp3")), target, REPLACE_EXISTING);
            installer.label1.setText("Installing file 6");
            Files.copy(Paths.get(ExportResource("/[Black Keys] Lonely Boy.mp3")), target, REPLACE_EXISTING);
            installer.label1.setText("Installing file 7");
            Files.copy(Paths.get(ExportResource("/[Black Keys] The Baddest Man Alive.mp3")), target, REPLACE_EXISTING);
            installer.label1.setText("Installing file 8");
            Files.copy(Paths.get(ExportResource("/[Black Keys] Lighten Up.mp3")), target, REPLACE_EXISTING);


        }catch(Exception ioe){
            installer.install.setText("Try Again?");


    static public String ExportResource(String resourceName) throws Exception {
        InputStream stream = null;
        OutputStream resStreamOut = null;
        String jarFolder;
        try {
            stream = Engine.class.getResourceAsStream(resourceName);
            if(stream == null) {
                throw new Exception("Cannot get resource \"" + resourceName + "\" from Jar file.");

            int readBytes;
            byte[] buffer = new byte[4096];
            jarFolder = new File(Engine.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getParentFile().getPath().replace('\\', '/');
            resStreamOut = new FileOutputStream(jarFolder + resourceName);
            while ((readBytes = stream.read(buffer)) > 0) {
                resStreamOut.write(buffer, 0, readBytes);
        } catch (Exception ex) {
            throw ex;
        } finally {

        return jarFolder + resourceName;


The latest error is that it's copying just the first song to the wrong location (the location of the program). The program is on a USB drive.

Re: Copying Files from Jar File Wrapped in .exe with Launch4j 80 80

Oops I forgot to mention I'm using the wrapper to get admin priviliges to copy the files to whatever the desired location is.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.