给定现有的jar文件和源代码,如何更改代码?

时间:2017-01-03 21:03:50

标签: java jar

我正在使用从SourceForge下载的一些Java代码。我有源代码,但是用户被指示通过jar文件运行代码:

java -jar jarfile.jar -i inputfile.txt -o $outputdirectory

当然,我可以使用以下命令

查看jarfile.jar内的内容
jar tf jarfile.jar

以下是我的问题:我想在源代码中更改一些内容,然后运行。我该怎么做呢?用户通常是

(1)更改jarfile.jar中编译的java代码?这听起来像一团糟

(2)再次将源代码包装到jar文件中?

我如何获取源代码(我做了哪些更改)并创建一个新的.jar文件?

1 个答案:

答案 0 :(得分:1)

找到此代码here以提取JAR内容:

import java.io.*;
import java.util.*;
import java.util.jar.*;
import java.util.zip.ZipException;

public class jara {
    public static void main (String args[])throws IOException,ZipException
    {
       JarFile jarFile = new JarFile("jarfile.jar");
       Enumeration en = jarFile.entries();
       while (en.hasMoreElements()) {
         String ent=proc(en.nextElement());
         if(ent.indexOf("/")>0)
         {
         String fil=ent.substring(0,ent.indexOf("/"));
         System.out.println(fil);
         File local=new File(fil);
         if(!local.exists())
         local.mkdirs();
         }
         if(ent.indexOf(".")>0)
         {
         int n=ent.length();
         String fil1=ent.substring(ent.lastIndexOf("/")+1,n);
         System.out.println(fil1);
          extract(jarFile.getName(),ent);  
         }

        }
     }

       public static String proc(Object obj)
       {
       JarEntry entry = (JarEntry)obj;
       String name = entry.getName();
       System.out.println("\nEntry Name: "+name);
       return(name);
       }

       public static void extract(String jarName,String entryName)throws IOException,ZipException
      {
      JarFile jar = new JarFile(jarName);
          System.out.println(jarName + " opened.");

          try {
             // Get the entry and its input stream.

             JarEntry entry = jar.getJarEntry(entryName);

             // If the entry is not null, extract it. Otherwise, print a 
             // message.

             if (entry != null) {
                // Get an input stream for the entry.

                InputStream entryStream = jar.getInputStream(entry);

                try {
                   // Create the output file (clobbering the file if it exists).

                   FileOutputStream file = new FileOutputStream(entry.getName());

                   try {
                      // Allocate a buffer for reading the entry data.

                      byte[] buffer = new byte[1024];
                      int bytesRead;

                      // Read the entry data and write it to the output file.

                      while ((bytesRead = entryStream.read(buffer)) != -1) {
                         file.write(buffer, 0, bytesRead);
                      }

                      System.out.println(entry.getName() + " extracted.");
                   }
                   finally {
                      file.close();
                   }
                }
                finally {
                   entryStream.close();
                }
             }
             else {
                System.out.println(entryName + " not found.");
             } // end if
          }
          finally {
             jar.close();
             System.out.println(jarName + " closed.");
          }
       }
     }

然后,只需将Java源文件读取为文本文件,修改它们,然后将它们写入新文件。

然后要打包jar,您可以使用this tutorial,它使用Java Compiler API