I need to convert a folder, that contains many sub folders to jar, using java. I'm a beginner in java. Please reply. I need a java program to convert a folder into a .jar
Asked
Active
Viewed 178 times
-5
-
4https://docs.oracle.com/javase/tutorial/deployment/jar/build.html – Prashant Jun 23 '15 at 12:23
-
http://www.javatpoint.com/how-to-make-an-executable-jar-file-in-java - check this... if you don't want it to be executable, just don't add the manifest file... – CoderNeji Jun 23 '15 at 12:25
-
Pack it as rar, rename it to jar. – Grim Jun 23 '15 at 12:25
-
Program to convert folder to jar: `${JAVA_HOME}/bin/jar` – MadConan Jun 23 '15 at 12:25
-
Do you need this at build time or at runtime? – Puce Jun 23 '15 at 12:27
-
thanks for replying.. i need it at compile time – abRam Jun 23 '15 at 12:32
-
Check this - [1]: http://stackoverflow.com/questions/2977663/java-code-to-create-a-jar-file – Ouney Jun 23 '15 at 12:32
-
How do you edit and compile Java code? Text editor, command line, IDE (NetBeans, Eclipse, IntelliJ,...) – Puce Jun 23 '15 at 12:40
1 Answers
-1
For complie time, you can use build tools like Apache Ant.
<jar destfile="${dist}/lib/app.jar">
<fileset dir="${build}/classes" excludes="**/Test.class" />
<fileset dir="${src}/resources"/>
</jar>
For runtime - Try this. It worked for me. For others - this is my first attempt at it. Please post your comments because i can be wrong here:)
public class CreateJar {
public static void main(String[] args) throws IOException {
String filePath = "/src";
List<File> fileEntries = new ArrayList<>();
getAllFileNames(new File(filePath), fileEntries);
JarOutputStream jarStream = new JarOutputStream(new FileOutputStream(new File("a.jar")));
for(File file : fileEntries){
jarStream.putNextEntry(new ZipEntry(file.getAbsolutePath()));
jarStream.write(getBytes(file));
jarStream.closeEntry();
}
jarStream.close();
}
private static byte[] getBytes(File file){
byte[] buffer = new byte[(int) file.length()];
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new FileInputStream(file));
//Read it completely
while((bis.read(buffer, 0, buffer.length))!=-1){
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return buffer;
}
private static void getAllFileNames(File file,List<File> list){
if(file.isFile()){
list.add(file);
}else{
for(File file1 : file.listFiles()){
getAllFileNames(file1, list);
}
}
}
}

Ouney
- 1,164
- 1
- 10
- 22
-
-
But he mentioned - "I need a java program to convert a folder into a .jar"? – Ouney Jun 23 '15 at 13:16
-
Guys downvoting should mention the reason, otherwise how will i come to know about the problems here?? I expect SO community to be transparent – Ouney Jun 23 '15 at 13:24
-
But I did mention that the OP is asking something else - hence the downvote. – Puce Jun 23 '15 at 13:47
-
-
But this is something, OP should be penalized for. Not everybody is expected to read all the comments!! I do not agree to this downvote , although i cant help it :( – Ouney Jun 23 '15 at 14:00
-
If the question is not clear enough, then we should ask the OP in the comments section first. The OP got many downvotes as well since the question is not clear. I suggest to wait and see until the OP provides all the information. If the OP stands to the statement "i need it at compile time" then I suggest you delete your answer so you don't lose reputation points. – Puce Jun 23 '15 at 14:03
-
hi, im not using ant as the build tool, i am using maven ..but i tried the runtime code that you provided and it is working perfectly...so i changed the scope to runtime.Thanks Ouney for the code – abRam Jun 24 '15 at 12:27
-