2

can anyone suggest me a way to have a running JAR file copy itself to a specific directory?

Thank you

Here's what I am trying:

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class ahker {
  public static void main(String[] args) throws IOException {
    File inputFile = new         
File(ahker.class.getProtectionDomain().getCodeSource().getLocation().getFile());
    File outputFile = new File("C:\\TEST.jar");

    FileReader in = new FileReader(inputFile);
    FileWriter out = new FileWriter(outputFile);
    int c;

    while ((c = in.read()) != -1)
      out.write(c);

    in.close();
    out.close();
  }
}

It is giving me the following compilation error:

Exception in thread "main" java.io.FileNotFoundException: 
C:\Users\---------------\bin (Access is denied)
Batzi
  • 369
  • 2
  • 7
  • 15
  • I have read about Quine but that's totally a different concept that won't achieve my goal. Having a program to output its source code is something and having a program copying itself is totally different. The array that contains the source code can only be used once so if someone compiles the output and runs it again, that program will not output its code again. – Batzi Nov 16 '11 at 06:01
  • [This solution][1] will copy jar file as well as content inside jar file. [1]: http://stackoverflow.com/questions/1386809/copy-a-directory-from-a-jar-file/2993908#2993908 – Muhammad Imran Tariq Nov 16 '11 at 06:48

2 Answers2

3
MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath()

this is your jar file's path.
and you can copy file using this--> http://download.oracle.com/javase/tutorial/essential/io/copy.html

Files.copy(sourcePath, targetPath, REPLACE_EXISTING);

or this-->

File inputFile = new File("input.txt");
File outputFile = new File("output.txt");

FileReader in = new FileReader(inputFile);
FileWriter out = new FileWriter(outputFile);
int c;

while ((c = in.read()) != -1)
  out.write(c);

in.close();
out.close();
shift66
  • 11,760
  • 13
  • 50
  • 83
  • Thanks for the code but I just noticed that the first line outputs the JAR's path but this doesn't solve my problem. I still need to copy that running JAR to the output path. – Batzi Nov 16 '11 at 06:35
  • Two examples shows you how to copy a file.Just write gotten JAR's path instead of sourcePath in first example or "input.txt" in second. – shift66 Nov 16 '11 at 06:41
  • it is giving me a "FileNotFoundException" as if it was checking for the JAR file when it has not been created yet. – Batzi Nov 16 '11 at 06:46
  • 1
    45 people say it's working... http://stackoverflow.com/questions/320542/how-to-get-the-path-of-a-running-jar-file – shift66 Nov 16 '11 at 06:50
  • try to use getFile() instead of getPath()It will return your JAR File and you can use it as inputFile in second example – shift66 Nov 16 '11 at 06:53
  • I re-edited my original post and included the code. Check it out. It's still giving me an error. – Batzi Nov 16 '11 at 07:05
  • Sorry Batzi, I don't know why it's not working.Print in console that path or gotten File's absolute path and I think it will help you to understand the issue – shift66 Nov 16 '11 at 07:13
  • o, if access is denied I think the problem is in Windows.I can't say exactly why that error occurs.You may not have permissions to copy to that directory or something else – shift66 Nov 16 '11 at 07:20
1

This solution will copy jar file as well as content inside jar file.

public void copyResourcesRecursively(URL originUrl, File destination) throws Exception {
    URLConnection urlConnection = originUrl.openConnection();
    if (urlConnection instanceof JarURLConnection) {
        copyJarResourcesRecursively(destination, (JarURLConnection) urlConnection);
    } else if (urlConnection instanceof FileURLConnection) {
        FileUtils.copyFilesRecusively(new File(originUrl.getPath()), destination);
    } else {
        throw new Exception("URLConnection[" + urlConnection.getClass().getSimpleName() +
                "] is not a recognized/implemented connection type.");
    }
}

public void copyJarResourcesRecursively(File destination, JarURLConnection jarConnection ) throws IOException {
    JarFile jarFile = jarConnection.getJarFile();
    for (JarEntry entry : CollectionUtils.iterable(jarFile.entries())) {
        if (entry.getName().startsWith(jarConnection.getEntryName())) {
            String fileName = StringUtils.removeStart(entry.getName(), jarConnection.getEntryName());
            if (!entry.isDirectory()) {
                InputStream entryInputStream = null;
                try {
                    entryInputStream = jarFile.getInputStream(entry);
                    FileUtils.copyStream(entryInputStream, new File(destination, fileName));
                } finally {
                    FileUtils.safeClose(entryInputStream);
                }
            } else {
                FileUtils.ensureDirectoryExists(new File(destination, fileName));
            }
        }
    }
}

Refer Here

Community
  • 1
  • 1
Muhammad Imran Tariq
  • 22,654
  • 47
  • 125
  • 190