Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify a string in Java bytecode (jar)

I want to modify a connection string that's hard-coded in a Java application (jar without source).

I presume it's possible to decompile the jar, then change the source and recompile to a new jar, but is there a quicker way to do this?

EDIT:

  • It's a standalone application, not a jar I'm loading from my own code
  • I doubt it will have been obfuscated: niche scientific application, author AWOL.
  • A solution that entails modifying the string "in memory" while the app is running would also suffice, but not ideal
like image 706
pufferfish Avatar asked Feb 12 '26 18:02

pufferfish


2 Answers

In your application that uses that jar, you could use reflection to set the connection String. (Reflection can be used even with private setters).

Obviously, if the setter is public, you could just call it without reflection.

like image 170
b.roth Avatar answered Feb 14 '26 09:02

b.roth


I think decompilation is probably the quickest way, provided the code hasn't been obfuscated such that a decompilation/compilation round-trip is not possible. You're going to have to decompile the code anyway to find the connection string, so you're half-way there already.

More importantly, you can take advantage of this method to pull the connection string out into a properties file, and hence (hopefully) only perform the decompilation once!

like image 22
Brian Agnew Avatar answered Feb 14 '26 08:02

Brian Agnew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!