我一般都是:jar -cvf HelloWorld.jar *.class,其他的没有怎么用过

解决方案 »

  1.   

    应该是: jar cfm HelloWorld.jar manifest.mf *.class
      

  2.   

    是不是没有删除manifest.mf这个文件。
    不应该这个文件是打到jar中的。^_^
    现在搞定了吗?
      

  3.   

    现在包含与不包含manifest.mf都可以用了,
    但是现在又有了其他的问题原来没有生成.jar文件的时候用
    java zipview test.zip
    可以正常使用,但是现在用的话,会有
    D:\javacode\Chp15-File and Stream\ZipExample of Web>java zipview test.zip
    Exception in thread "main" java.lang.NoClassDefFoundError: zipview
    的错误,请问为什么?
    而我生成.jar文件后用
    D:\javacode\Chp15-File and Stream\ZipExample of Web>java -jar zipview.jar test.zip
    却没有问题?
    对此甚是不解,各位赐教
    zipview的源码:import java.io.*;
    import java.util.Enumeration;
    import java.util.zip.*;
       
    public class zipview {
      public static void dump(ZipFile zf, ZipEntry ze)
        throws IOException
      {
        System.out.println(">>>>> " + ze.getName());
        InputStream istr = zf.getInputStream(ze);
        BufferedInputStream bis =
          new BufferedInputStream(istr);
        FileDescriptor out = FileDescriptor.out;
        FileOutputStream fos = new FileOutputStream(out);
        int sz = (int)ze.getSize();
        final int N = 1024;
        byte buf[] = new byte[N];
        int ln = 0;
        while (sz > 0 &&  // workaround for bug
          (ln = bis.read(buf, 0, Math.min(N, sz))) != -1) {
            fos.write(buf, 0, ln);
            sz -= ln;
         }
         bis.close();
         fos.flush();
       }
       
       public static void main(String args[])
       {
         boolean dump_contents = false;
         int arg_indx = 0;
         ZipFile zf = null;
       
         if (args.length >= 1 && 
             args[0].equals("-contents")) {
           dump_contents = true;
           arg_indx++;
         }
         if (arg_indx >= args.length) {
           System.err.println("usage: [-contents] file");
           System.exit(1);
         }
       
         try {
           zf = new ZipFile(args[arg_indx]);
         }
         catch (ZipException e1) {
           System.err.println("exception: " + e1);
         }
         catch (IOException e2) {
           System.err.println("exception: " + e2);
         }
       
         Enumeration list = zf.entries();
         while (list.hasMoreElements()) {
           ZipEntry ze = (ZipEntry)list.nextElement();
           if (!dump_contents || ze.isDirectory()) {
             System.out.println(ze.getName());
             continue;
          }
          try {
            dump(zf, ze);
          }
          catch (IOException e) {
            System.err.println("exception: " + e);
          }
        }
      }
    }
      

  4.   

    看看你的zipview.jar中的META-INF里边的manifest.MF,能直接java -jar执行,MF文件中肯定有main-class,如果打包的时候不指定manifest.mf的位置,打出来的包直接执行会提示找不到Main-class,就是运行类。如果没有指定mf文件,你用 java -classpath "zipview.jar" zipview test.zip 应该可以执行
    [前提是你的zipview没有在某个package下,如果在某个package下,java -classpath "zipview.jar" package.zipview test.zip ]如果打包的时候指定mf文件后,java -jar zipview test.zip 没问题的