0

Yes, I am aware that there are questions similar to mine, but I believe mine is slightly more specific. I have made a program but the people using it are unable to compile it either due to not having the jdk or not knowing how to (I have tried explaining in more detail but with no success), I have decided that the next best option is making a "generator" which generates the finished jar file for them.

I do this by simply compiling a base jar file myself and uploading that to a server. The generator downloads my compiled jar file and replaces certain strings with the user's input. The main problem I am having is that I am unable to simply edit the downloaded file without messing with something, I have not been able to figure out how to do this, so far I have attempted reading the file's content replacing it, and rewriting it to a file, which is where my problem comes, since in the original there are multiple classes and packages, but when writing it to a file using Files.write() it completely ignores that. The code I have tried so far is

Path downloaded_temp_file = Paths.get("temp.jar");

Charset charset = StandardCharsets.UTF_8;

String temp_file_content = new String(Files.readAllBytes(downloaded_temp_file), charset);
String new_file_content = temp_file_content.replaceAll("easy_to_find_and_replace", "this_is_actually_the_user_input");
Files.write(Paths.get("no_longer_temp.jar"), new_file_content.getBytes(charset));

I do not get any error but the file goes from 19kb > 34kb, cannot be opened by winrar, when opened in notepad some strings can be extracted but nothing useful.

PK
    ��rR              me/��  PK
    ��rR                  me/ruben/PK  ��rR               me/ruben/Main$1.class�U[WW���0$��تM-�p
 J+ rmCJ�@�=$�08��3Jj�>��>��U�}�K�����>�/�W��O�}�p���Yk�e�=�|��ߙ������A5.`TcrWq*F0Y�)|��#

where me/ruben is the package, Main.class is the main class file.

I have now told my story, in summary my question is: How do I replace a string within a jar file from another java program.

Any answer or reference to other source helps, thank you.

Ruben
  • 15
  • 3
  • a jar is a zip archive. You cannot just change arbitrary binary data somewhere within it, chances are you will break checksums, file offsets, lengths, etc. If anything you can try to unzip the file, change the individual java class files and re-zip it. Changing the class file seems to generally be possible without the need for recompilation: https://stackoverflow.com/questions/7859984/changing-javas-class-file-without-recompiling – luk2302 Mar 18 '21 at 20:28
  • 1
    A jar file is an archive and cannot be read as if it was a plain text file. You can adapt from this post in SO https://stackoverflow.com/questions/1529611/how-to-write-a-java-program-which-can-extract-a-jar-file-and-store-its-data-in-s – aksappy Mar 18 '21 at 20:28
  • Why don't you just pass the string as argument when starting your jar ? Or use an external text file as resource ? – Arthur Attout Mar 18 '21 at 20:32
  • Sounds like your jar needs to read from a config file with the user's input.... – Roddy of the Frozen Peas Mar 18 '21 at 20:32

0 Answers0