Include exe file in Project Netbeans Java

I want to include a file in my project in Netbeans, I am developing a PC application with Java language. I have searched almost on the web but found nothing. When I compile the application, if I enter the path where / dist is, the exe file is missing here. Thank you very much.

String exec [] = {getClass().getClassLoader().getResource("inc_volume.exe").getPath() };
                System.out.println(exec[0]);
                Runtime.getRuntime().exec(exec);

      

Update on 08/20/2014 15.29

I found this source to extract from the jar, but I don't know how to use it:

    java.util.jar.JarFile jar = new java.util.jar.JarFile(jarFile);
java.util.Enumeration enumEntries = jar.entries();
while (enumEntries.hasMoreElements()) {
    java.util.jar.JarEntry file = (java.util.jar.JarEntry) enumEntries.nextElement();
    java.io.File f = new java.io.File(destDir + java.io.File.separator + file.getName());
    if (file.isDirectory()) { // if its a directory, create it
        f.mkdir();
        continue;
    }
    java.io.InputStream is = jar.getInputStream(file); // get the input stream
    java.io.FileOutputStream fos = new java.io.FileOutputStream(f);
    while (is.available() > 0) {  // write contents of 'is' to 'fos'
        fos.write(is.read());
    }
    fos.close();
    is.close();
}

      

Here's an image:

enter image description here

+3


source to share


1 answer


To include an exe file in your project, copy this exe file through the file system to src

your Netbeans project folder .

exe file into your project

when you created your project, this exe file will be packaged into the project jar file.

exe file packaged into jar file

At runtime, to run this exe you will need to extract this exe file from the jar file .

And when this exe file is extracted, you can execute it.



To run an external application from your java code, I recommend using Apache Commons Exec: http://commons.apache.org/proper/commons-exec/


UPDATE

Below is an example class to demonstrate how to extract all exe files from the current executable jar. I used these SO posts to make this class: first and second .

import java.io.File;
import java.io.IOException;

/**
 *
 */
public class TestClass {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {

        extractExeFiles("C://Temp");

    }


    /**
     * Gets running jar file path.
     * @return running jar file path.
     */
    private static File getCurrentJarFilePath() {
        return new File(TestClass.class.getProtectionDomain().getCodeSource().getLocation().getPath());
    }

    /**
     * Extracts all exe files to the destination directory.
     * @param destDir destination directory.
     * @throws IOException if there an i/o problem.
     */
    private static void extractExeFiles(String destDir) throws IOException {
        java.util.jar.JarFile jar = new java.util.jar.JarFile(getCurrentJarFilePath());
        java.util.Enumeration enumEntries = jar.entries();
        String entryName;
        while (enumEntries.hasMoreElements()) {
            java.util.jar.JarEntry file = (java.util.jar.JarEntry) enumEntries.nextElement();
            entryName = file.getName();
            if ( (entryName != null) && (entryName.endsWith(".exe"))) {
                java.io.File f = new java.io.File(destDir + java.io.File.separator + entryName);
                if (file.isDirectory()) { // if its a directory, create it
                    f.mkdir();
                    continue;
                }
                java.io.InputStream is = jar.getInputStream(file); // get the input stream
                java.io.FileOutputStream fos = new java.io.FileOutputStream(f);
                while (is.available() > 0) {  // write contents of 'is' to 'fos'
                    fos.write(is.read());
                }

                fos.close();
                is.close();                
            }
        }
    }
}

      

+4


source







All Articles