modify properties file in META-INF - java

I'm using maven and storing a properties file in src/main/resources which I can read fine like:
properties.loadFromXML(this.getClass().getClassLoader().
getResourceAsStream("META-INF/properties.xml");
Is it possible to then write to this file when it is packaged up as a jar? I've tried the following:
properties.storeToXML(new FileOutputStream(new File(this.getClass().getClassLoader().
getResource("META-INF/properties.xml").toURI())), "");
This works in Eclipse because it is saving the file to target/classes/META-INF, but not when packaged as a jar, so is it possible to achieve the same thing?

Is it possible to then write to this file when it is packaged up as a jar?
Short answer: no, it is not possible, a jar file is a file, not a directory. And actually, you generally don't write properties files back to a jar file.
You should put that properties file on the classpath on the local file system, outside a JAR.

It's always possible to modify a packaged file by unpacking, rewriting and re-packing. Sometimes this is the easiest approach.
A Jar file is essentially a renamed .ZIP file. Java has classes for accessing files within a .Zip file, and you could (if sufficiently motivated) write yourself a program to do this.
Alternatively, I'm pretty sure there are ant tasks that can do this too (think creative use of the jar task), and there are POM plugins available to run ant tasks from Maven.

Related

how to update a params in a jar file?

I need to update a config.params in within a jar file that was compiled by ant. I know I don't need to re-compile the source java code for doing this. Can someone help me with how to update the params file in jar file?
I need to change an integer value n the config.params file:
fileSize = 4 should be changed to fileSize = 20
Try extracting jar in a zip program like 7-Zip (or rename to .zip and open with Windows), change the file data, then re-zip (and rename back to jar if you need to).
Technically the "compiling" was done by javac (not Ant) to build .class files, which you don't need to touch since the developers were smart enough to put the setting you need into a properties file.
I did the following:
jar xf jar-file [archived-file(s)]
modified the file I wanted
jar cf jar-file input-file(s)
learned from http://docs.oracle.com/javase/tutorial/deployment/jar/build.html
an even easier way is this: jar uf jar-file input-file(s)
http://docs.oracle.com/javase/tutorial/deployment/jar/update.html (no need to touch the jar file at all)

Writing A Text File To A Jar

I'm writing a program which 'builds' another. I plan to save the settings insite the generated jar file, but am having some trouble with writing to an external jar. Any help is appreciated.
A jar is just a zip file so all you have to do is zip up your files and name the zip with a .jar extension.
One handy library to create jar archives is Shrinkwrap which allows you to put classes, resources, etc into jar files using a Java API
A better solution would be to create a second jar, and write your own ClassLoader to load classes from that jar. A kind of plugin mechanism.

How should resources in a compiled jar be accessed?

First of all, I have read through many S.O. questions regarding this topic and I have tried the things suggested in them.
Here is my situation. I am writing a Java app using the Processing framework and I'm in the final stages where I need to begin thinking about packaging the app. A jar file that is executable from the command line is what I'm attempting to build using the Export feature in Eclipse.
The structure of my project looks like this:
src/
multiple packages/
libs/
jar files and natives
data/
fonts and images
config/
json files
When I export the jar file and uzip the jar to inspect it's contents, I find that the contents of these dirs have been dumped in the top level of the .jar.
Which looks like this:
.jar
packages
jar files
fonts
json files
So, when I attempt to load a config file with something like:
BufferedReader reader = new BufferedReader( new FileReader( path ) );
Everything works just file when I run the app in Eclipse. But the jar file throws a FileNotFoundException.
Many of the questions that I've seen on S.O. regarding problems like these recommend using using class.getClass().getResource() or class.getResourceAsStream(). I have tried both of these using relative paths and just the file name as in:
class.getResource( 'config.json' );
class.getResources( 'cfg/config.json' );
class.getResourceAsStream( '../../config.json' );
All of these methods return null, when run from either Eclipse or the jar using:
java -jar myjarfile.jar
I am also open to using an Ant file. In fact, I'm now using the Ant file generated by the export feature to build the jar. If there is something I can add to that to add the directories into the jar that would be great too.
To reach resources in the root, prepend a / to the path. If not, you do it relative to the current package, which is usually only useful if the resource is physically next to the class in your sources too.
So use class.getResourceAsStream("/config.json"); to reach config.json in the root of the jar.
Also note that jars-inside-jars are not directly supported.
Your .jar file should just include the directories related to the "package" for the compiled code. You might be referencing a .war structure with /lib /WEB-INF etc. and this is different.
If your package structure is:
com.yourco.authentication
And your class in Login
Then your jar should be
/com/
/yourco/
/authentication/
Login.class
you then need the .jar in your classpath for the env to run via command line.
I see you note it works in Eclipse which likely has environment settings and imported required libs, etc. so hard to tell exactly. If your packages/ folder includes the compiled java code, I'm unsure if that'll work when referenced externally, thus suggesting you start your packages in the root folder.

how to obfuscate the war file using proguard

I want to obfuscate a war file using proguard,how can i do so ?
Please explain me the steps
I'm puzzled by this question.
As other Java obfuscators, Proguard obfuscates the compiled bytecode (.class files) by renaming every variable, method etc. it considers safe to rename. I suppose you know this and know how to use Proguard for class files since you specifically asked about it. If not, read the proguard manual.
As for war files, you can extract the class files from war (or jar) by unzipping it. (yes, a war is a normal zip file). Then you can run them through Proguard and zip it up again. You can use Winzip, unzip or whatever zip program you prefer. You can even zip and unzip the war with JDK (jar -xvf tobe_extracted.war).
This obfuscation does not affect the other files inside the war, like properties files, xml files and such. The war is not encrypted. Your software may not work after this because reflection based stuff may be break. Proguard can't know whether your frameworks are going to access something with their mighty reflection magic and if it's actually safe to rename it.

Modifying a jar file

I have a jar file which is used in html file as applet. I want to modify the content of the jar file and to rebuild the jar file so that the html will work fine with the new jar file. How can i do this??
I already tried unzipping using 7zip nad modified the source and created the new jar. But when i use it in html it shows some java.lang.Classnotfound error
You can unjar or rejar the classes and source files as you wish.
unjar
jar -xvf abc.jar
jar
jar cf abc.jar input-files
http://docs.oracle.com/javase/tutorial/deployment/jar/build.html
Make the changes in the code (.java files), recompile to get the .class files. Then simply replace the old .class files in the jar with the new ones. I usually use WinZip, but you can use whatever app that can handle .Zip files. It should just work.
I've faced cases where the launcher of the app uses some sort of verification and checks for this kind of changes. I had to use a new launch script. This doesn't seem to be your case though.
This is surely possible from the command line. Use the u option for jar
From the Java Tutorials:
jar uf jar-file input-file(s)
"Any files already in the archive having the same pathname as a file being added will be overwritten."
See Updating a JAR File
A brief test shows this quickly updates changes apart from trying to delete the file.
I haven't seen this answer on other threads about modifying jar files, and many, marked as duplicates, suggest there is no alternative but to remake the jar completely. Please correct if wrong.
JARs are just ZIP files, use whatever utility you like and edit away!
Disclaimer: When reverse engineering any code be sure that you are staying within the limits of the law and adhering to the license of that code.
Follow the instructions above to unpack the JAR.
Find the original source of the JAR (perhaps its on SourceForge) and download the source, modify the source, and rebuild your own JAR.
You can also decompile the class files in the JAR. This is a rather advanced process and has a lot of "gotchas".

Categories

Resources