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

  • 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: http://www.homeandlearn.co.uk/java/java.html –  Aug 20 '14 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? – Lorenzo Sogliani Aug 20 '14 at 11:55
  • 1
    @PeterHorvath i don't know, there are a stupid people in this community – Lorenzo Sogliani Aug 20 '14 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!!! – Am_I_Helpful Aug 20 '14 at 12:07
  • @LorenzoSogliani please check my answer I have just posted with pictures. –  Aug 20 '14 at 12:16
  • @LorenzoSogliani take care with the negative labeling, that does not work in your favor. Fact of the matter is that plenty of clueless people ask how to "convert" a java class to an exe and I'll bet people were defensively voting down assuming that was the case here too. The way you ask your question -really- invites to assume that too, "after I compile the exe is not there". You never clarify what "the exe" really is unless you happen to get it from the vague subject. – Gimby Aug 20 '14 at 12:27

1 Answers1

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();                
            }
        }
    }
}
Community
  • 1
  • 1
  • 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"); – Lorenzo Sogliani Aug 20 '14 at 13:02
  • 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: http://stackoverflow.com/questions/13991007/execute-external-program-in-java –  Aug 20 '14 at 13:08
  • and if you insist on removing your exe from jar at runtime, then check this article: http://www.jguru.com/faq/view.jsp?EID=68627 –  Aug 20 '14 at 13:10
  • i don't understand how to use this source, that you have sent me with the link – Lorenzo Sogliani Aug 20 '14 at 13:23
  • http://stackoverflow.com/questions/1529611/how-to-write-a-java-program-which-can-extract-a-jar-file-and-store-its-data-in-s – Lorenzo Sogliani Aug 20 '14 at 13:25
  • that code extracts all contents of jar file, you do not need to extract all contents, just iterate over jar entries as it is shown in that code, and extract only your exe file as you get the corresponding jar entry. As you get an jar entry, there's a method: `getName()`, use it to determine, when you get your exe file packaged into this jar, and extract it. And just ignore all other entries. –  Aug 20 '14 at 13:27
  • I have updated the original post with the code that i want to know how to use and image of src of my project. can you see its? Yes, but I did not understand where I have to put this code to make sure that the files are extracted – Lorenzo Sogliani Aug 20 '14 at 13:32
  • @LorenzoSogliani moment, please. I will update my answer shortly with example code. –  Aug 20 '14 at 14:00
  • Thank you so much, i have just created another app java with the code to extract my other file jar but at the moment of the extraction tells me that access is denied to the file (exe) @Rafael Osipov – Lorenzo Sogliani Aug 20 '14 at 14:03
  • @LorenzoSogliani check the update I just posted to my answer. There's sample working code, to extract exe files from the current running jar file. –  Aug 20 '14 at 14:04
  • i have tested and it's correct code,but when you extracted the files exe tells me access denied or controlled well through cmd. @Rafael Osipov – Lorenzo Sogliani Aug 20 '14 at 14:10
  • 1
    nothing it's work we have solved. THANK YOU SO MUCH :) @Rafael Osipov – Lorenzo Sogliani Aug 20 '14 at 14:15
  • @LorenzoSogliani How you solved the access denied issue ? I am having the same problem. – captaindroid Dec 08 '16 at 10:25