代码之家  ›  专栏  ›  技术社区  ›  Lorenzo Sogliani

在Project Netbeans Java中包含文件exe

  •  3
  • Lorenzo Sogliani  · 技术社区  · 11 年前

    我想在我的Netbeans项目中包含一个文件,我正在用Java语言为PC开发一个应用程序。我几乎在网上搜索,但一无所获。当我编译应用程序时,如果我进入存在/dist的路径,则文件exe不在这里。 非常感谢。

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

    2014年8月20日更新15.29

    我找到了从jar中提取的这个源,但我不知道如何使用:

        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();
    }
    

    此处图片:

    enter image description here

    1 回复  |  直到 11 年前
        1
  •  4
  •   Community Mohan Dere    9 年前

    要将exe文件包含到项目中,请通过文件系统将此exe文件复制到 src Netbeans项目的文件夹。

    exe file into your project

    构建项目后,这个exe文件将打包到项目jar文件中。

    exe file packaged into jar file

    在运行时,您需要 to extract this exe file from your jar file .

    当提取这个exe文件时,您可以执行它。

    要从java代码启动外部应用程序,我建议使用Apache Commons Exec: http://commons.apache.org/proper/commons-exec/


    更新

    下面的示例类演示如何从当前运行的jar文件中提取所有exe文件。我使用这些SO帖子来制作这个类: the first the 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'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();                
                }
            }
        }
    }
    
    推荐文章