Extract a JAR file, possibly with WINRAR on CL - java

I need to be able to extract jar files on the command line.
Piece of cake, you might say. Yes, except I need the extractor to rename same-named files.
To be specific, the Jar file has a file named:
classAX.class
and another named:
classax.class
in the same directory. I need it to extract them both, and preferably rename one:
classax (2).class
or something similar.
It must be able to do this without any user intervention.
Winrar has this capability, but when attempting to extract with unrar.exe, it says that the input file is not a valid RAR archive. (Though winrar has jar capabilities.)
Any way to force winrar to accept jars via command line, or perhaps another program?
Thanks,
~Kurt Nauck

You can develop your own programme in java, there is a Zip package that can deal with compressed files, an also you can deal with a rest of your needs

7Zip command line util accomplishes this perfectly. Thanks for the answers.

Related

Extract contents of Windows executable that is a wrapped jar file?

I've been trying to unwrap a jar file that is in a Windows executable. I tried to open it with 7Zip, but as I expected that didn't work, and going through the executable in a hex editor leads me to believe it was wrapped with exe4j. Is there any way to get the contents of the jar file?
So I figured out the solution, it turns out exe4j puts the jar file it needs into a user's temporary directory, like this. I'll leave this up for anyone else who may need to something like this.

Importing Jar files in another directory within your jar file

I have a Jar file that has the a few packages.
I want to import the commons library and within Eclipse that's pretty simple.
However when I export the jar file, and run it, it is not able to find the commons library and therefore unable to run. I want to know if I have to move the commons jar files to the exact same directory (within the package)? Or if there is something I can add to the import lines to make it work?
I also want to make this jar file able to be sent to people and used by anyone without having to get them to modify anything on their computer
you need to include jars in your classpath if you are running on command line (Here is an example of this)
if you want it to all be neatly wrapped in one jar consider looking at OneJar. (oneJar example) or JarJar.

Convert .jar file to JAVA DOC

How can I put my jar file to web. i.e is there a software that decompile jar file and make html pages of it?
In other words I want to make a java doc.
[Edit according to user comment]
So you want to extract javadoc from a jar...
First you must understand that if your jar doesn't contain the sources, but only the compiled code that your javadoc will not show any comment.
Then you just need to extract the file in your jar using any zip program (for exemple on windows, rename file to .zip, and extract it).
Last thing to do, is to call the javadoc tool on it. Like other said you can use an IDE for that, or simply call from the command line :
http://download.oracle.com/javase/1.5.0/docs/tooldocs/windows/javadoc.html#examples
javadoc -d \home\html -sourcepath \home\src -subpackages java -exclude java.net:java.lang
Creating Javadoc the way it was intended works, as far as I know, only from sources. If the JAR file also contains the source files, then it's just a matter of writing a Java program that opens the JAR file and puts any source files through the javadoc utility.
Interesting idea, but I don't know of anything doing that at the moment. To my knowledge it is however possible to generate Javadoc from inside of java program.
Another approach would be to load the classes into your classpath and use reflection to figure out methods, fields, etc. It will give you a rough overview of the classes, but sadly not the detailed stuff "normal" javadoc generation gives you. A lot of information is discarded upon compilation.
Converting .jar file to html seems impossible, except if you intend to users to download the .jar file from the web. However, html to .jar is possible.
I am certain that only the java source can be converted to javadoc. See here to convert java source to javadoc.
I have created a JAVADOC from a jar file that is not created by you i.e you don't have its source code.
Its simple but tricky.
Get source code from .jar file by using java decompiler. I use JD-GUI | Java Decompiler
Make a new project in myEclipse and copy the 'source code' in source folder.
In project menu you will see Generate Javadoc. Click and select you project and follow the steps. Java doc will be ready after you finish it.
Cheers
Imran Tariq
Once you downloaded your APK file , You need to do the following steps to get a editable java code/document.
Convert your apk file to zip (while start your download don't go
with save option , just go with save as and mention your
extension as .zip) by doing like this you may avoid APKTOOL...
Extract the zip file , there you can find somefilename.dex. so
now we need to convert dex -> .class
To do that, you need dex2jar(you can download it from
here , after extracted, in
command prompt you have to mention like,
[here] (Keep in mind that your somefilename.dex must be inside the same folder where you
have keep your dex2jar.)
Download jad from
http://www.viralpatel.net/blogs/download/jad/jad.zip and
extract it. Once extracted you can see two files like "jad.exe" and
"Readme.txt" (sometimes "jad.txt" may there instead of "jad.exe", so
just rename its extension as.exe to run)
Finally, in command prompt you have to mention like [D:\jad>jad
-sjava yourfilename.class] it will parse your class file into editable java document.
Use mvn javadoc:javadoc in 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".

Putting bat file inside a jar file

I have a java class that uses a bat file to execute commands. However I developed it in Eclipse IDE. It works fine in there. But as I export it in a jar file, it fails to find the bat file that was included.(gives me an IOException)
The file structure in eclipse is as follows
:
Project1
---->src
------>com.myproj
-------->BatFileRead.java
----md.bat
----ul.bat
md.bat and ul.bat is same level as src directory. After jarring it src folder disappears.
Could someone help me with this.
Thanks
In order to execute the command, you'll have to extract the bat file afterwards. You can't run executables which are inside jar files. Basically you'll need to open the batch file entry in the jar file as an input stream, and copy the data to a FileOutputStream on disk. You won't be able to execute it until it's a proper standalone file on the file system.
If you're already trying to extract it, chances are you're using getResource or getResourceAsStream slightly incorrectly. This is easy to do, because it depends whether you're calling ClassLoader.getResourceAsStream or Class.getResourceAsStream. The first only ever uses absolute paths (implicitly) and the second can use either absolute or relative paths. For example, in your case you'd want:
BatFileRead.class.getResourceAsStream("/md.bat")
or
BatFileRead.class.getClassLoader().getResourceAsStream("md.bat")
Have you checked that the bat files are definitely ending up in the jar file? Just list the contents with
jar tvf file.jar
to see what's in there.
Well this can be very dangerous. Be sure to use gloves when dealing with the BAT. They bite and quite painfull. Also try getting jar that has a big enough opening, although the bats will fit almost through any hole.
Good luck, and don't try this at home. This is done by professionals.
Try copying file.jar to file.zip, and opening file.zip (e.g. just double clicking, or using e.g. 7-Zip). Can you find your .bat files inside? If the .bat file isn't there, you have to add the .bat file to your Eclipse project, and rebuild in Eclipse. If the .bat file is there, but you still get an IOException opening it from you .java application, then please post the code snippet triggering the IOException, and please give a full listing of file.zip (get the listing by extracting file.zip to C:\testdir, and running dir /s C:\testdir in the command line).
Please note that Jon Skeet is right that although it is possible to open any file in the .jar file as an InputStream using java.lang.Class.getResourceAsStream and java.lang.ClassLoader.getResourceAsStream (see tutorial or find them in the Java API docs), but in order to execute a .bat file inside, you have to extract the .jar file first.

Categories

Resources