有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java给定一个现有的jar文件和源代码,如何修改代码?

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

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

当然,我可以通过以下命令看到jarfile.jar中的内容

jar tf jarfile.jar

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

(1)更改jarfile中编译的java代码。罐子听起来像是一团糟

(2)将源代码重新包装到jar文件中

我如何获取源代码(以及我所做的更改)并创建一个新的.jar文件


共 (1) 个答案

  1. # 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