-5

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

abRam
  • 67
  • 9

1 Answers1

-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
  • The OP is looking for a build time solution, not a runtime solution. – Puce Jun 23 '15 at 13:13
  • 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
  • Have a look at the comments. – Puce Jun 23 '15 at 13:48
  • 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
  • You can use maven-antrun-plugin to write ant code in maven as well. – Ouney Jun 24 '15 at 13:17