Writing A Text File To A Jar - java

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.

Related

Extracting java class files from jar

I had running executable jar of a Java project. Unfortunately I have lost all the source code but I still got the executable jar of it. Is there any possibility to extract my classes from the jar?
I have tried extracting but class files are in damaged shape.
To get source from .class files , just download a decompiler from here and get them.
Are you talking about decompiling or just receiving the class files? The class files can be extracted using any software capable of reading ZIP. If you talk about decompiling that usually will work purly. But you could try Jad which I had the best experience with.

What is the counterpart of java jar in msbuild?

I have been using apache ant to compile my java programs for quite a while, the problem now is that we have to learn to program in the .Net Framework.
I have been struggling with the building process of my csproj in ms build. The requirement we have was to create a build file of a program using only the VisualStudio Command Prompt and notepad++ to create the proj file.
Is there any task in msbuild where i can package my files similar to java jar? I searched within the MsBuild task reference but haven't found anything yet. Any help would be appreciated, thanks!
A JAR file allows Java runtimes to efficiently deploy a set of classes and their associated resources. The elements in a JAR file can be compressed, which, together with the ability to download an entire application in a single request, makes downloading a JAR file much more convenient than separately downloading the many uncompressed files which would form a single Java Application.
When you build a Dotnet application, you usually get a set of files (one .exe and multiple dlls as the simple scenario).
These files being "zipped into one common file" ... that concept does not exist in DotNet.
(except for Silverlight, but that's a different story...that's a "xap" file I believe).
Most people use a Msbuild Task to package their files together.
I use something like this:
MSBuild and creating ZIP files
to zip up my "binaries" on 1 zip file, and zip up my config files in a separate zip file.
That's a "poor man's" method...but it works.
So there is not a direct apples to apples comparison.
..........
That's the simple explanation.
When .dlls reside in the GAC, that's a different ball game.
EDIT::::::::
Here is an example of an extender library to zip files using MSBuild.
How do I zip a folder in MSBuild?
Here is 2 of the most common extension libraries
http://msbuildtasks.tigris.org/
and
http://msbuildextensionpack.codeplex.com/

Loading a .class file into a jar?

Is it possible to do any of the following?:
Create a new JAR file from a given .class file?
Insert a local .class file into an existing JAR?
It seems that I would need to somehow read the .class into a JarEntry, but this does not seem possible with any of the existing JarEntry constructors. Is there any workaround or alternative ways of accomplishing this?
Thanks in advance
EDIT: Looking for a programming solution
jar files are just zip files with classes in it. When you have java installed on your machine you can create jar files the jar command.
If you use a java IDE it should have a option to create a jar file from some classes.
But most people use a build tool to accomplish that. Well known and widely used java build tools are Apache Ant or Apache Maven. Apache Ant is probably better to start with if you're new to java and it's tooling.
Using .class files you can extract the source code using JAD decompiler. Then create a new JAR file of your own where you modify as per your needs.
I've written some utility methods to add files to ZIP/JAR files using the NIO2 File API (the library is Open Source):
Maven:
<dependency>
<groupId>org.softsmithy.lib</groupId>
<artifactId>softsmithy-lib-core</artifactId>
<version>0.3</version>
</dependency>
Tutorial:
http://softsmithy.sourceforge.net/lib/current/docs/tutorial/nio-file/index.html
API: CopyFileVisitor.copy

modify properties file in META-INF

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.

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