Loading a .class file into a jar? - java

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

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.

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.

C++ library equivalent for Java?

What would be the equivalent of a library in C++ for Java? I have some classes I'd like to reuse as a library in multiple projects.
Thanks
Basically you build a jar file with the classes in, and then add a reference to that jar file on the classpath for both compilation and execution.
See the Oracle Jar File Tutorial for more information.

create java library file

I'm using number of java files as common in different ADF projects, I want to archive them in jar file so I can import this archive file from any new project to use the java libraries in the project, I tried create it in jdeveloper but no luck.
Thank you for any advice,
You can use the following article to understand the way.
creating a Jar file
jar cf jar-file input-file(s)
But still you can zip a file using windows zip utility or winzip and rename it as a jar file. It is not official but it works.
No matter how big or small you project is, the best answer to this question IMHO is a build tool. I would recommend maven, it is a great tool. It will take some time to get into and it will probably slow down your pace at first but the rewards of knowing how to use a tool like that are very big.
http://maven.apache.org/
from your terminal - go to the directory where you have the files you want to jar and type the command
jar -cf myjarfile.jar *.java
hope this helps
see also:
Creating a JAR File
Assuming that the files that you want to reuse are related to ADF you should read about using ADF Libraries to increase reusability.
See: http://docs.oracle.com/cd/E16162_01/web.1112/e16182/reusing_components.htm#BEIGHHCG

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