简体   繁体   中英

Re-compile a Java Class from Jar

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.

Now I want to compile it and add the compiled class file into the jar and run it.

Not surprisingly I get multiple "cannot find symbol" errors for references of custom object that were in the jar file.

I tried to compile the file by referencing the jar file in the classpath like so

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?

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.

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:

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? 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

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):

  1. create a new jar by taking the old one and deleting the classfile that you want to replace
  2. compile the corrected java file and make sure that the modified jar is on the classpath
  3. add the newly compiled classfile to the jar

This should work. If not - ask the author for a new complete library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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