2011-11-16 1 views
1

누구나 특정 디렉토리에 실행중인 JAR 파일 자체를 복사 할 수있는 방법을 제안 할 수 있습니까? 파일 자체를 Java로 복사합니다.

가 여기에 내가 노력하고 무엇 감사 : 그것은 나에게 다음과 같은 컴파일 오류를주고있다

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) 
+0

나는 Quine에 대해 읽었습니다하지만 내 목표를 달성하지 않습니다 완전히 다른 개념입니다. 소스 코드를 출력하는 프로그램을 갖는 것은 무언가이며 프로그램 자체를 복사하는 것은 완전히 다릅니다. 소스 코드가 포함 된 배열은 한 번만 사용할 수 있습니다. 따라서 누군가가 출력을 컴파일하고 다시 실행하면 그 프로그램은 코드를 다시 출력하지 않습니다. – Batzi

+0

[이 해결책] [1]은 jar 파일과 jar 파일 내부의 내용을 복사합니다. 는 는 [1] : http://stackoverflow.com/questions/1386809/copy-a-directory-from-a-jar-file/2993908#2993908 코드에 대한 –

답변

3
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(); 
+0

감사하지만 난 그냥 눈치 그 첫 번째 줄은 JAR의 경로를 출력하지만 이것이 내 문제를 해결하지는 못합니다. 실행중인 JAR을 출력 경로에 복사해야합니다. – Batzi

+0

두 가지 예제는 파일을 복사하는 방법을 보여줍니다. 첫 번째 예에서는 sourcePath 대신 JAR의 경로를 쓰거나 두 번째 예에서는 "input.txt"를 작성합니다. – shift66

+0

JAR 파일이 아직 만들어지지 않았을 때 JAR 파일을 검사하는 것처럼 "FileNotFoundException"을 제공합니다. – Batzi

1

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