0

I have a program that needs to be able to create an executable JAR-file of itself, but I'm unfortunately having some trouble making it work. This is the method I'm currently using to create the JAR:

public static void createJar() throws IOException {
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, "JarTest");

    JarOutputStream jos = null;
    try {
       String jarPath = System.getProperty("user.dir") + "/Test.jar";
       jos = new JarOutputStream(new FileOutputStream(jarPath), manifest);
    }
    catch (IOException e) {e.printStackTrace();}

    ArrayList<String> fileList = new ArrayList<String>();
    String codeDir = System.getProperty("user.dir") + "/bin/jartest/";
    Files.list(Paths.get(codeDir)).forEach(entry -> {
        fileList.add(((Path)entry).toString());
    });

    int len = 0;
    byte[] buffer = new byte[1024];
    for(String file : fileList ) {
        //create JarEntry
        JarEntry je = new JarEntry(file);
        je.setComment("Creating Jar");
        je.setTime(Calendar.getInstance().getTimeInMillis());
        System.out.println(je);
        jos.putNextEntry(je);

        //write the bytes of file into jar
        InputStream is = new BufferedInputStream(new FileInputStream(file));
        while((len = is.read(buffer, 0, buffer.length)) != -1)
            jos.write(buffer, 0, len);
        is.close();
        jos.closeEntry();
        System.out.println("Done");
    }
    jos.close();
}

When I execute this no errors happen and I do get a JAR file called "Test.jar" generated in my Eclipse project folder. But when I open the JAR the .class files are not there, instead I see this:

enter image description here

I know that it's finding the .class files since the line System.out.println(je); successfully prints out the absolute path of each of them, so why aren't they getting put into the JAR?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Just some guy
  • 1,909
  • 4
  • 21
  • 32
  • It that some kind of exercise? Otherwise why not use the jar tool? – Henry Apr 09 '16 at 12:44
  • It's an exercise to create a self-replicating program, like a computer worm. – Just some guy Apr 09 '16 at 12:48
  • If you do `jar tvf Test.jar`, does it show expected entries and sizes? – Kedar Mhaswade Apr 09 '16 at 13:04
  • @KedarMhaswade Yes, it does list the .class files with the correct sizes. So they are inside the JAR after all? But I cannot run it with 'java -jar Test.jar' (main class not found), and I cannot see any .class files when I open it with WinRAR. – Just some guy Apr 09 '16 at 13:13
  • Duplicate? http://stackoverflow.com/questions/1281229/how-to-use-jaroutputstream-to-create-a-jar-file – Mike Apr 09 '16 at 13:14

1 Answers1

0

It solved itself when I changed these two lines:

String jarPath = System.getProperty("user.dir") + "/Test.jar";
String codeDir = System.getProperty("user.dir") + "/bin/jartest/";

To these:

String jarPath = "Test.jar";
String codeDir = "bin/jartest/";

I can now see the .class files inside the JAR.

Just some guy
  • 1,909
  • 4
  • 21
  • 32