-3

I have services.jar file and have few packages inside this jar file. I need to modify the code of one its java classes.

Example - services.jar

Packages inside services.jar - search , util

Classes inside search package - Filter.java - Need to edit this code and need to make a new jar file.

Please guide me.

jahroy
  • 22,322
  • 9
  • 59
  • 108
user2200589
  • 5
  • 1
  • 1
  • you can just use winzip to explore the jar and manually update files in the jar. – Sean F May 14 '13 at 00:26
  • Perhaps, you will have to close this question since there is http://stackoverflow.com/questions/12057554/creating-a-jar-file-after-doing-some-modifications-to-it. The only answer looks good to me. – lifus May 14 '13 at 00:28
  • 3
    Why you want to modify the java file inside the jar ? You should be updating the class file instead. – NullPointerException May 14 '13 at 00:30
  • Let's start with, noramlly Jars don't contain *.java files, nothing saying that they can't, just normally, they don't. So trying to extract a file from the Jar file would seem pointless - IMHO. *"Need to edit this code and need to make a new jar file*" is you answer. Take a look at [Packing Programs in JAR Files](http://docs.oracle.com/javase/tutorial/deployment/jar/) – MadProgrammer May 14 '13 at 00:30
  • To clarify NullPointerException's comment further: modifying a _.java_ source file will have no impact on the related _.class_ files and will therefore have no effect on how the jar file behaves when you run it (or compile with it). – jahroy May 14 '13 at 00:32
  • Take it out of the JAR file; modify it; put it back. Not a real question. – user207421 May 14 '13 at 01:04

3 Answers3

3

You can do so if you have the code available with you that you want to modify. If yes, then you have to extract the jar either from command line or using any tool such as winrar. Here is the command to extract the jar from command line:

jar -xvf services.jar

Replace the modified or updated /class file in the extracted directory and recreate the jar using the following command from commandline:

jar -cvf services.jar
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • There is also a `-u` option that can update a jar file in place. See [this answer](http://stackoverflow.com/a/14244548/778118) for an example. However, as others have mentioned, modifying a _.java_ file won't have any effect on the compiled class files. – jahroy May 14 '13 at 00:36
1

You should not be updating the java file. Jar file is for archiving the compiled java classes

Instead you should be updating the class file after making the changes

Refer this article

NullPointerException
  • 3,732
  • 5
  • 28
  • 62
0

If you have to, you can shadow the jar file with another placed ahead of it on the class path. However, if you do not understand what you are doing, and you miss some dependencies, you are likely to get a NoClassDefFoundError.

If you need to modify part of a class's behavior, you could weave in an aspect with AspectJ. You might use an Around aspect if you want to replace the behavior.

Eric Jablow
  • 7,874
  • 2
  • 22
  • 29