Java 相当于 jar cf 的代码

Java code equivelent of jar cf

我正在编写一个 java 程序,它获取我生成的 .class 文件并将它们打包,然后使用 class 加载程序将它们加载到内存中。

我目前有一个可用的震动系统,代码为:

 public static int BUFFER_SIZE = 10240;
  protected static void createJarArchive(File archiveFile, File[] tobeJared) {
    try {
      byte buffer[] = new byte[BUFFER_SIZE];
      // Open archive file
      FileOutputStream stream = new FileOutputStream(archiveFile);
      JarOutputStream out = new JarOutputStream(stream, new Manifest());

      for (int i = 0; i < tobeJared.length; i++) {
        if (tobeJared[i] == null || !tobeJared[i].exists()
            || tobeJared[i].isDirectory())
          continue; // Just in case...
        System.out.println("Adding " + tobeJared[i].getName());

        // Add archive entry
        JarEntry jarAdd = new JarEntry(getPackageNameModified +tobeJared[i].getName());
        jarAdd.setTime(tobeJared[i].lastModified());
        out.putNextEntry(jarAdd);

        // Write file to archive
        FileInputStream in = new FileInputStream(tobeJared[i]);
        while (true) {
          int nRead = in.read(buffer, 0, buffer.length);
          if (nRead <= 0)
            break;
          out.write(buffer, 0, nRead);
        }
        in.close();
      }

      out.close();
      stream.close();
      System.out.println("Adding completed OK");
    } catch (Exception ex) {
      ex.printStackTrace();
      System.out.println("Error: " + ex.getMessage());
    }
  }

引用代码中的内容:

getPackageNameModified = com\Test

但是,当我将 jar 加载到内存中时,出现以下错误:

Could not load jar C:\Users\Dalton\AppData\Local\Temp\Test.jar into JVM. (Could not find class com.Test.ObjectFactory.)

我正在使用 squoop class 加载程序来执行此操作。

又名:

ClassLoaderStack.addJarFile(jarFileAbsolute, absoluteClass);

使用以下命令行 class 加载程序代码可以正常工作,但我想避免在 java 程序中使用命令行:

jar cf C:\Users\Dalton\AppData\Local\Temp\Test.jar -C C:\Users\Dalton\AppData\Local\Temp\Test\com.Test com\Test

以下解决了我的问题:

try
    {
        byte buffer[] = new byte[BUFFER_SIZE];
        // Open archive file
        FileOutputStream stream = new FileOutputStream(archiveFile);
        JarOutputStream out = new JarOutputStream(stream, new Manifest());

        for (int i = 0; i < tobeJared.length; i++)
        {
            if (tobeJared[i].toString().endsWith(CLASS))
            { 
                // the .replace \ with / is a java JDK bug that requires all
                    // paths to use / and end in / for a jar to properly be made

                LOG.info("Adding "
                        + getPackageNameModified().replace(
                                DOUBLE_BACKSLASH, FORWARD_SLASH) + FORWARD_SLASH + tobeJared[i].getName()
                        + " to the temporary JAR.");

                // Add archive entry
                JarEntry jarAdd = new JarEntry(
                        getPackageNameModified().replace(DOUBLE_BACKSLASH,
                                FORWARD_SLASH) + FORWARD_SLASH + tobeJared[i].getName());
                jarAdd.setTime(tobeJared[i].lastModified());
                out.putNextEntry(jarAdd);

                // Write file to archive
                FileInputStream in = new FileInputStream(tobeJared[i]);
                while (true)
                {
                    int nRead = in.read(buffer, 0, buffer.length);
                    if (nRead <= 0) break;
                    out.write(buffer, 0, nRead);
                }
                in.close();
            }
        }

        out.close();
        stream.close();
        LOG.info("Adding complete --> Success");
    }
    catch (Exception ex)
    {
        ex.printStackTrace();
        LOG.error("Error: " + ex.getMessage());
    }

问题在 java \ 是一个错误。所有目录拆分都使用 / 代替,并以 / 结尾。我不知道为什么会出现这个问题,但正因为如此,生成的 JAR 可以 class 加载。