简体   繁体   English

从Jar重新编译Java类

[英]Re-compile a Java Class from Jar

I have an executable jar that has one class file with a wrong value. 我有一个可执行jar,它有一个类值错误的类文件。 I requested that file from the author and corrected the value. 我向作者请求了该文件并更正了该值。

Now I want to compile it and add the compiled class file into the jar and run it. 现在我想编译它并将已编译的类文件添加到jar中并运行它。

Not surprisingly I get multiple "cannot find symbol" errors for references of custom object that were in the jar file. 毫不奇怪,我为jar文件中的自定义对象的引用获得了多个“找不到符号”错误。

I tried to compile the file by referencing the jar file in the classpath like so 我尝试通过引用类路径中的jar文件来编译该文件,就像这样

C:/> javac  file.java  -classpath C:/folder/where/jar/is

but this doesnt seem to work... I get the same errors as if just doing 但这似乎没有用......我得到了同样的错误

C:/> javac file.java

Is there a way to compile this individual class somehow referencing the other files in the jar? 有没有办法编译这个单独的类以某种方式引用jar中的其他文件?

Thanks. 谢谢。


Errors I am getting while following some of the suggestions below here: 我在遵循以下一些建议时遇到的错误:

javac -classpath C:/jar/location.jar File.java

File.java:226: cannot find symbol
symbol  : class Stuff
location: class com.shared.stuffers
                              Stuff s1 = new Stuff();
                                             ^

Stuff class is found in the Jar, but can not be seen by the javac program... I feel like I am doing something wrong but not sure where? 在Jar中可以找到Stuff类,但javac程序无法看到...我觉得我做错了但不确定在哪里? Thanks. 谢谢。

You will want to compile your file like so: 您将要编译您的文件,如下所示:

javac -classpath C:\folder\where\jar\is\the_jar_file.jar file.java

per the javac usage instructions: 根据javac使用说明:

C:\Console2>javac -help
Usage: javac <options> <source files>

编译完新文件后(例如在Will先生的回答中),您可以使用以下命令将新文件添加到jar中:

jar uf C:\folder\where\jar\is\the_jar_file.jar file.class

您可能必须指定JAR文件本身而不仅仅是它所在的目录。

javac file.java -classpath C:\folder\where\jar\is\the_jar_file.jar

I'm just guessing, but did you check whether it doesn't need any external jar libraries you may have to include in your compilation command? 我只是在猜测,但你是否检查过它是否不需要你可能必须包含在编译命令中的任何外部jar库? Another thing you could do is to compile all of the classes by doing something like 你可以做的另一件事是通过做类似的事情来编译所有的类

javac *.java ...

As others have mentioned, once you have the .class file recompiled you need to replace the older version in the .jar 正如其他人所提到的,一旦重新编译了.class文件,就需要替换.jar中的旧版本

You'll likely need to have any compile time dependencies available to rebuild this class. 您可能需要有任何编译时依赖项来重建此类。 If it's an open source project this could be an easy thing to come up with. 如果它是一个开源项目,这可能是一件容易的事情。 If not, it's more difficult. 如果没有,那就更难了。 If the author sent you the file he can probably help you with this as well. 如果作者向您发送了该文件,他也可以帮助您。 You might be able to get the author to produce a patched distribution for you as well. 您也许可以让作者为您生成修补的发行版。 Odds are he/she already has the build environment set up and this should be relatively easy to do. 可能性是他/她已经建立了构建环境,这应该相对容易。

I'd try this approach (and it should work, unless the 'debugged' class doesn't introduce a new error): 我尝试这种方法(它应该工作,除非'debugged'类不引入新的错误):

  1. create a new jar by taking the old one and deleting the classfile that you want to replace 通过获取旧jar并删除要替换的类文件来创建新jar
  2. compile the corrected java file and make sure that the modified jar is on the classpath 编译更正的java文件,并确保修改后的jar在类路径上
  3. add the newly compiled classfile to the jar 将新编译的类文件添加到jar中

This should work. 这应该工作。 If not - ask the author for a new complete library. 如果不是 - 请求作者提供一个新的完整库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM