3

i want to include a file into my project in Netbeans, i'm developping an application for PC with the language Java. I searched almost on the Net, but i have found nothing. When i compile the application if i go into path where there is /dist the file exe aren't here. Thank you so much.

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

Update on 20/08/2014 15.29

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

    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 Image:

enter image description here

6
  • Lorenzo, when you build java project then you have jar file as a result, in the dist folder. If you have included a file into your project it is encapsulated into this jar file. If you are not familiar with java I recommend to study the basics first. Otherwise you will meet many problems which will be hard to overcome. Start here: homeandlearn.co.uk/java/java.html Commented Aug 20, 2014 at 11:25
  • @RafaelOsipov how to include file exe into project so that when I compile the file and I get the jar is all together? Commented Aug 20, 2014 at 11:55
  • 1
    @PeterHorvath i don't know, there are a stupid people in this community Commented Aug 20, 2014 at 11:56
  • Why not include the project and .exe file together into a jar file by deploying that project and try the same then!!! Commented Aug 20, 2014 at 12:07
  • @LorenzoSogliani please check my answer I have just posted with pictures. Commented Aug 20, 2014 at 12:16

1 Answer 1

4

To include an exe file to your project, copy this exe file via filesystem to the src folder of your Netbeans project.

exe file into your project

when you have built your project, then 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 your jar file.

And as this exe file is extracted you can execute it.

To launch an external application from your java code I recommend to use Apache Commons Exec: http://commons.apache.org/proper/commons-exec/


UPDATE

Below there's sample class to demonstrate how to extract all exe files from the current running jar file. I used these SO posts to make this class: the first and the second ones.

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's 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();                
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

13 Comments

ok I was able to add the jar file to exe file, now how do I remove them automatically or to use them to run using the command: Runtime.getRuntime (). Exec ("inc_volume.exe");
I do not recommend to remove exe from jar file as it is packaged and is necessary for your application. If you remove it as it is extracted, then this jar file cannot be executed if this exe file is deleted or transferred to another folder. If exe is always included, you can extract it to a temp folder, execute and delete this exe file. Regarding launching files, I prefer to use Apache Commons Exec over calling Runtime.getRunime.exec(). If you don't want to use Apache Commons Exec, check this post please: stackoverflow.com/questions/13991007/…
and if you insist on removing your exe from jar at runtime, then check this article: jguru.com/faq/view.jsp?EID=68627
i don't understand how to use this source, that you have sent me with the link
nothing it's work we have solved. THANK YOU SO MUCH :) @Rafael Osipov
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.