Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JARCF的Java代码等价物_Java_Classloader_Executable Jar_.class File - Fatal编程技术网

JARCF的Java代码等价物

JARCF的Java代码等价物,java,classloader,executable-jar,.class-file,Java,Classloader,Executable Jar,.class File,我正在编写一个java程序,该程序获取生成的.class文件并将其JAR,然后使用类加载器将其加载到内存中 我目前有一个工作的jarring系统,代码如下: public static int BUFFER_SIZE = 10240; protected static void createJarArchive(File archiveFile, File[] tobeJared) { try { byte buffer[] = new byte[BUFFER_SIZE

我正在编写一个java程序,该程序获取生成的.class文件并将其JAR,然后使用类加载器将其加载到内存中

我目前有一个工作的jarring系统,代码如下:

 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());
    }
  }
但是,当我将jar加载到内存中时,我得到一个错误:

Could not load jar C:\Users\Dalton\AppData\Local\Temp\Test.jar into JVM. (Could not find class com.Test.ObjectFactory.)
我正在使用一个squoop类加载器来实现这一点

又名:

使用以下命令行,类加载器代码可以正常工作,但我希望避免在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());
    }
试试看
{
字节缓冲区[]=新字节[缓冲区大小];
//打开存档文件
FileOutputStream=新的FileOutputStream(archiveFile);
JarOutputStream out=新的JarOutputStream(stream,new Manifest());
for(int i=0;i如果(nRead)您确实使用了
jar tf
来验证程序创建的.jar文件的内容,对吗?jar(和zip)条目的名称使用/作为路径分隔符,并在最后一个包(名义上是目录)之间包含一个分隔符组件和basename.FYI:Windows路径名可以使用/或\在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());
    }