zxqmnzi 2010-07-22 09:32
浏览 271
已采纳

如何把swing 写的程序弄成可以安装的文件

我的情况是这样子的
我用C写了个dll文件,然后SWING程序需要调用DLL文件,我需要swing程序打包成可以安装的文件,不光是单纯的exe
DLL文件需要放到windows system32下面去.

请问大家怎么用?

如果单纯的打包成exe文件,则不行,因为那样子的话,DLL文件就不能用了

请大家帮下忙,谢谢

  • 写回答

1条回答 默认 最新

  • huntor 2010-07-22 10:21
    关注

    dll 文件放到 System32 目录不是必须的。
    sqlitejdbc的 dll文件就是打包到jar文件里。
    看其中的 SQLiteJDBCLoader.java
    [code="java"]package org.sqlite;

    import java.io.BufferedInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.URL;
    import java.security.DigestInputStream;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    import java.util.Properties;

    /**

    • Set the system properties, org.sqlite.lib.path, org.sqlite.lib.name,
    • appropriately so that the SQLite JDBC driver can find *.dll, *.jnilib and
    • *.so files, according to the current OS (win, linux, mac).
    • The library files are automatically extracted from this project's package
    • (JAR).
    • usage: call {@link #initialize()} before using SQLite JDBC driver.
    • @author leo
    • */
      public class SQLiteJDBCLoader
      {

      private static boolean extracted = false;

      public static boolean initialize() {
      loadSQLiteNativeLibrary();
      return extracted;
      }

      static boolean getPureJavaFlag() {
      return Boolean.parseBoolean(System.getProperty("sqlite.purejava", "false"));
      }

      public static boolean isNativeMode() {
      if (getPureJavaFlag())
      return false;

      // load the driver
      initialize();
      return extracted;
      

      }

      /**

      • Computes the MD5 value of the input stream
      • @param input
      • @return
      • @throws IOException
      • @throws NoSuchAlgorithmException
        */
        static String md5sum(InputStream input) throws IOException {
        BufferedInputStream in = new BufferedInputStream(input);

        try {
        MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
        DigestInputStream digestInputStream = new DigestInputStream(in, digest);
        for (; digestInputStream.read() >= 0;) {

        }
        ByteArrayOutputStream md5out = new ByteArrayOutputStream();
        md5out.write(digest.digest());
        return md5out.toString();
        

        }
        catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException("MD5 algorithm is not available: " + e);
        }
        finally {
        in.close();
        }
        }

      /**

      • Extract the specified library file to the target folder
      • @param libFolderForCurrentOS
      • @param libraryFileName
      • @param targetFolder
      • @return
        */
        private static boolean extractAndLoadLibraryFile(String libFolderForCurrentOS, String libraryFileName,
        String targetFolder) {
        String nativeLibraryFilePath = libFolderForCurrentOS + "/" + libraryFileName;
        final String prefix = "sqlite-" + getVersion() + "-";

        String extractedLibFileName = prefix + libraryFileName;
        File extractedLibFile = new File(targetFolder, extractedLibFileName);

        try {
        if (extractedLibFile.exists()) {
        // test md5sum value
        String md5sum1 = md5sum(SQLiteJDBCLoader.class.getResourceAsStream(nativeLibraryFilePath));
        String md5sum2 = md5sum(new FileInputStream(extractedLibFile));

            if (md5sum1.equals(md5sum2)) {
                return loadNativeLibrary(targetFolder, extractedLibFileName);
            }
            else {
                // remove old native library file
                boolean deletionSucceeded = extractedLibFile.delete();
                if (!deletionSucceeded) {
                    throw new IOException("failed to remove existing native library file: "
                            + extractedLibFile.getAbsolutePath());
                }
            }
        }
        
        // extract file into the current directory
        InputStream reader = SQLiteJDBCLoader.class.getResourceAsStream(nativeLibraryFilePath);
        FileOutputStream writer = new FileOutputStream(extractedLibFile);
        byte[] buffer = new byte[1024];
        int bytesRead = 0;
        while ((bytesRead = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, bytesRead);
        }
        
        writer.close();
        reader.close();
        
        if (!System.getProperty("os.name").contains("Windows")) {
            try {
                Runtime.getRuntime().exec(new String[] { "chmod", "755", extractedLibFile.getAbsolutePath() })
                        .waitFor();
            }
            catch (Throwable e) {}
        }
        
        return loadNativeLibrary(targetFolder, extractedLibFileName);
        

        }
        catch (IOException e) {
        System.err.println(e.getMessage());
        return false;
        }

      }

      private static synchronized boolean loadNativeLibrary(String path, String name) {
      File libPath = new File(path, name);
      if (libPath.exists()) {

          try {
              System.load(new File(path, name).getAbsolutePath());
              return true;
          }
          catch (UnsatisfiedLinkError e) {
              System.err.println(e);
              return false;
          }
      
      }
      else
          return false;
      

      }

      private static void loadSQLiteNativeLibrary() {
      if (extracted)
      return;

      boolean runInPureJavaMode = getPureJavaFlag();
      if (runInPureJavaMode) {
          extracted = false;
          return;
      }
      
      // Try loading library from org.sqlite.lib.path library path */
      String sqliteNativeLibraryPath = System.getProperty("org.sqlite.lib.path");
      String sqliteNativeLibraryName = System.getProperty("org.sqlite.lib.name");
      if (sqliteNativeLibraryName == null)
          sqliteNativeLibraryName = System.mapLibraryName("sqlitejdbc");
      
      if (sqliteNativeLibraryPath != null) {
          if (loadNativeLibrary(sqliteNativeLibraryPath, sqliteNativeLibraryName)) {
              extracted = true;
              return;
          }
      }
      
      // Load the os-dependent library from a jar file
      sqliteNativeLibraryPath = "/native/" + OSInfo.getNativeLibFolderPathForCurrentOS();
      
      if (SQLiteJDBCLoader.class.getResource(sqliteNativeLibraryPath + "/" + sqliteNativeLibraryName) == null) {
          // use nested VM version
          return;
      }
      
      // temporary library folder
      String tempFolder = new File(System.getProperty("java.io.tmpdir")).getAbsolutePath();
      /* Try extracting the library from jar */
      if (extractAndLoadLibraryFile(sqliteNativeLibraryPath, sqliteNativeLibraryName, tempFolder)) {
          extracted = true;
          return;
      }
      
      extracted = false;
      return;
      

      }

      private static void getNativeLibraryFolderForTheCurrentOS() {
      String osName = OSInfo.getOSName();
      String archName = OSInfo.getArchName();

      }

      public static int getMajorVersion() {
      String[] c = getVersion().split("\.");
      return (c.length > 0) ? Integer.parseInt(c[0]) : 1;
      }

      public static int getMinorVersion() {
      String[] c = getVersion().split("\.");
      return (c.length > 1) ? Integer.parseInt(c[1]) : 0;
      }

      public static String getVersion() {

      URL versionFile = SQLiteJDBCLoader.class.getResource("/META-INF/maven/org.xerial/sqlite-jdbc/pom.properties");
      if (versionFile == null)
          versionFile = SQLiteJDBCLoader.class.getResource("/META-INF/maven/org.xerial/sqlite-jdbc/VERSION");
      
      String version = "unknown";
      try {
          if (versionFile != null) {
              Properties versionData = new Properties();
              versionData.load(versionFile.openStream());
              version = versionData.getProperty("version", version);
              version = version.trim().replaceAll("[^0-9\\.]", "");
          }
      }
      catch (IOException e) {
          System.err.println(e);
      }
      return version;
      

      }

    }
    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。