【问题标题】:File copies itself in Java文件在 Java 中复制自身
【发布时间】:2011-12-30 03:24:40
【问题描述】:

谁能给我建议一种让正在运行的 JAR 文件将自身复制到特定目录的方法?

谢谢

这是我正在尝试的:

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();
  }
}

它给了我以下编译错误:

Exception in thread "main" java.io.FileNotFoundException: 
C:\Users\---------------\bin (Access is denied)

【问题讨论】:

  • 我读过关于 Quine 的文章,但那是完全不同的概念,无法实现我的目标。让程序输出其源代码和让程序复制自己是完全不同的。包含源代码的数组只能使用一次,所以如果有人编译输出并再次运行它,该程序将不会再次输出它的代码。
  • [此解决方案][1] 将复制 jar 文件以及 jar 文件中的内容。 [1]:stackoverflow.com/questions/1386809/…

标签: java jar copy file-copying


【解决方案1】:
MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath()

这是您的 jar 文件的路径。
您可以使用此复制文件--> http://download.oracle.com/javase/tutorial/essential/io/copy.html

Files.copy(sourcePath, targetPath, REPLACE_EXISTING);

或者这个-->

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();

【讨论】:

  • 感谢代码,但我只是注意到第一行输出了 JAR 的路径,但这并不能解决我的问题。我仍然需要将运行中的 JAR 复制到输出路径。
  • 两个示例向您展示如何复制文件。只需在第一个示例中写入获取的 JAR 路径而不是 sourcePath 或在第二个示例中写入“input.txt”。
  • 它给了我一个“FileNotFoundException”,就好像它正在检查尚未创建的 JAR 文件一样。
  • 尝试使用 getFile() 而不是 getPath() 它会返回你的 JAR 文件,你可以在第二个例子中使用它作为 inputFile
【解决方案2】:

This solution 将复制 jar 文件以及 jar 文件中的内容。

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

【讨论】:

    猜你喜欢
    • 2012-10-23
    • 1970-01-01
    • 2014-10-04
    • 1970-01-01
    • 1970-01-01
    • 2015-10-29
    • 2013-12-26
    • 1970-01-01
    • 2017-04-24
    相关资源
    最近更新 更多