20

Hi im busy on a application that decompiles a jar the pastes files into the folder of the decompiled jar, it then compresses the folder into a jar.

Decompiling and copying works, but i can't manage to get the folders contents to be jared (compressed into jar), i did about 3hrs research and found only outdated methods. please help.

-Regards marko5049

EDIT MORE INFO: I apologize i mean i cant get my application to turn a folder into a jar file, my application is an modification installer for a jar file. and it extracts the jars files, then adds the modification and then, is supposed to then turn the folder back into a jar file so that the modification is installed. The jar file is not executable.

marko5049
  • 257
  • 1
  • 2
  • 7
  • By decompiling, I guess you mean extracting the files from the jar. If you want an answer, define "I can't manage". What are you doing, and what happens when you do it? Show your code of cammands. – JB Nizet Aug 17 '13 at 11:38
  • What does "i can't manage to get the folders contents to be jared" mean? Did you run into an error? A problem using the JAR? Please be clear. – Jason C Aug 17 '13 at 11:45
  • I apologize i mean i cant get my application to turn a folder into a jar file, my application is an modification installer for a jar file. and it extracts the jars files, then adds the modification and then, is supposed to then turn the folder back into a jar file so that the modification is installed. The jar file is not executable. – marko5049 Aug 17 '13 at 12:14
  • @marko5049: we won't write the code for you. Show us the code you tried, tell us what you expect it to do, and what it does instead. We need code, stack traces, example input and output. Concrete things. "I can't manage" and "I can't get" are not clear descriptions of the problem you're facing. – JB Nizet Aug 17 '13 at 12:52
  • @marko5049 Is it acceptable to update the JAR yourself and redistribute the fully updated JAR instead of automating it on the user's side? – Jason C Aug 17 '13 at 13:13
  • Duplicate of OP's already-closed question: http://stackoverflow.com/questions/18282902/java-copying-into-a-jar-file – Jason C Aug 17 '13 at 13:23

9 Answers9

17

This worked for me for a MAC OSX:

Open Terminal at the folder with the jar file and run the following commands

 unzip mylib.jar -d jarfolder 

 //You can then change whatever you need and finally run the command below

 jar cvf mylib.jar -C jarfolder/ .
Sugoi Reed
  • 457
  • 5
  • 3
11

Given that you want to create the JAR through code; you can use JarOutputStream for that. There is an example at this link that contains code to create a JAR file given a File[] containing all the input files.

As for creating the list of files given a starting input path, see Recursively list files in Java.

You could either build a list of files then just use code like in the above example, or you could recursively scan files and add them to the JAR as you go.

If you are using Java 7 and you know your users are too you can also use Files.walkFileTree() with a FileVisitor that adds entries to the JAR as it visits files.


Original answer before OP clarified:

Is there something wrong with:

jar cf my-application.jar folder1 folder2 folder3 etc

The JDK comes with a jar utility to create JAR files.

You can read an official tutorial on it here: Creating JAR Files. It is very straightforward.

If you want to create a runnable JAR, you can create a manifest file that has the main class and other options in it. The linked tutorial describes that process.

Community
  • 1
  • 1
Jason C
  • 38,729
  • 14
  • 126
  • 182
  • Thanks. i'll search up. – marko5049 Aug 17 '13 at 11:46
  • No need for a search; just click the tutorial link in my post! :) What is the actual problem you are having? – Jason C Aug 17 '13 at 11:51
  • I need to turn a folder into a jar file, not manually though, i need to do it using code, as i am making an installer for a modification i made to the jar. – marko5049 Aug 17 '13 at 12:04
  • So make your code run `jar cf output.jar sourcedir`. Virtually every language I know of that has the capability to automate an installation also has the capability to run a command line tool. – Jason C Aug 17 '13 at 12:31
  • 1
    @JasonC: jar comes with the JDK. Most people don't have the JDK installed. Only the JRE. Fortunately, the standard Java API has support for zip and jar files manipulation. – JB Nizet Aug 17 '13 at 12:51
  • Good point. Plus there's the added difficulty of making sure jar is in your path. I think OpenJDK's jar tool runs standalone and can be redistributed but there's plenty of difficulties with that too. I wonder if the OP can't just distribute the updated JAR? – Jason C Aug 17 '13 at 12:59
  • @JasonC I have a file chooser. – marko5049 Aug 17 '13 at 23:05
8

The short answer is, ZIP the folder, then rename it to a JAR file.

Shahid Hussain Abbasi
  • 2,508
  • 16
  • 10
4

The easiest way to make it .. put your folder to C:\Program Files\Java\jdk1.8.0_221\bin and then reach till the same path from CMD then run this

jar cvf Name_your_jar.jar folder

CGK
  • 2,662
  • 21
  • 24
3

for windows just make the folder as winrar file.,

  • to do this right click the folder and click "7 -zip" then
  • choose "add to foldername.zip".
  • now a rar file is created with the same folder name.
  • Then open the cmd in current folder directory
  • type "mv foldername.zip foldername.jar"

Now you got the executable jar file with your corresponding folder.

Dici
  • 25,226
  • 7
  • 41
  • 82
VijayVishnu
  • 537
  • 2
  • 11
  • 34
  • 1
    these steps is not working in windows.. 'mv' is not recognized as an internal or external command.. so we can create using unix tools like cygwin or gitbash – Ranjith Sekar May 20 '17 at 11:33
2

Following command worked for me in Windows 10 and jdk-8u212

jar cf my-application.jar folder1 folder2 folder3 etc
devman
  • 496
  • 5
  • 24
1

You can put your files in a zip folder. Then convert the zip file into Jar format.A .jar extension file is a Java Archive format file. It is used to store a large amount of files into one single file. You can try a free online file converter without downloading a new software on your computer. There are various online file converters available on Google. I would recommend Convert zip to jar

I hope it helps.

anna anna
  • 11
  • 1
0

I just found this question and its answers are more useful for your problem: how to zip a folder itself using java

2 tips :

1、a jar is exactly a zip. So, you just need to zip your folder, and rename it to jar

2、be careful that you should zip your whole folder without changing the relative path of the files, but not just extract all the .class files and zip them together. Because when you run the jar, the class package should be consistent with its path.

learner
  • 1,381
  • 1
  • 10
  • 16
0

I suggest trying to create a regular .ZIP file in Windows. You need to get 7-zip in order to view the .JAR file you are creating. You should just paste contents into the .ZIP, then rename the file type from .ZIP to .JAR, this worked for me and I hope this works for you.

.JARs are basically .ZIPs created by the Oracle Java client, so you need special file viewing software such as 7-zip or WinRAR to view it for some reason.

You can also revert .JARs to .ZIPs by renaming the file type. You might have to mod your computer with RegeEdit or something to have access to renaming your file types.

I hope this helps.