从 Jar 重新编译 Java 类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/946071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Re-compile a Java Class from Jar
提问by Powerlord
I have an executable jar that has one class file with a wrong value. I requested that file from the author and corrected the value.
我有一个可执行的 jar,它有一个值错误的类文件。我向作者索取了该文件并更正了该值。
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? Thanks.
在Jar中找到了stuff类,但是javac程序看不到...我感觉我做错了什么但不知道在哪里?谢谢。
回答by Mr. Will
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>
回答by Powerlord
Once you've compiled the new file (such as in Mr. Will's answer), you can add the new file to the jar using:
编译新文件后(例如在 Will 先生的回答中),您可以使用以下命令将新文件添加到 jar 中:
jar uf C:\folder\where\jar\is\the_jar_file.jar file.class
回答by Adam Paynter
You probably have to specify the JAR file itself and not just the directory it resides in.
您可能必须指定 JAR 文件本身,而不仅仅是它所在的目录。
javac file.java -classpath C:\folder\where\jar\is\the_jar_file.jar
回答by Juri
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? Another thing you could do is to compile all of the classes by doing something like
我只是在猜测,但是您是否检查过它是否不需要您可能必须在编译命令中包含的任何外部 jar 库?您可以做的另一件事是通过执行类似的操作来编译所有类
javac *.java ...
回答by cwash
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.
您可能需要有任何可用于重建此类的编译时依赖项。如果它是一个开源项目,这可能是一件容易的事情。如果没有,那就更难了。如果作者将文件发送给您,他可能也可以帮助您。您也可以让作者为您制作补丁发行版。很可能他/她已经设置了构建环境,这应该相对容易。
回答by Andreas Dolk
I'd try this approach (and it should work, unless the 'debugged' class doesn't introduce a new error):
我会尝试这种方法(它应该可以工作,除非“调试”类没有引入新错误):
- create a new jar by taking the old one and deleting the classfile that you want to replace
- compile the corrected java file and make sure that the modified jar is on the classpath
- add the newly compiled classfile to the jar
- 通过使用旧的并删除要替换的类文件来创建一个新的 jar
- 编译更正的java文件并确保修改后的jar在类路径上
- 将新编译的类文件添加到 jar 中
This should work. If not - ask the author for a new complete library.
这应该有效。如果没有 - 向作者索要一个新的完整库。

