Bitte beachten Sie, dass es wichtig ist, dass die Klasse (YourMainClass
Referenz im Code), die den folgenden Code ausgeführt wurde aus der JAR-Datei geladen wird, die Sie kopieren möchten:
String jarFilePath = YourMainClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();
Wie Sie bekommen jar-Datei-Pfad, können Sie in ein Verzeichnis copy it:
import static java.nio.file.StandardCopyOption.*;
Files.copy(source, target, REPLACE_EXISTING);
UPDATE
Um das Illegal char <:> at index 2:
Problem, berichtet in den Kommentaren, statt FileSystems.getDefault().getPath()
Aufruf Verwendung java.io.File
Klasse zu erhalten Path
Objekt über toPath()
Verfahren zu vermeiden.
Ich habe den Code aktualisiert und den Zielordnerpfad vom Code zum Anwendungsargument verschoben.
Der vollständige Code ist unten:
import java.io.File;
import java.io.IOException;
import static java.nio.file.StandardCopyOption.*;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
public class JarFileCopyTester {
public static void main(String[] args) throws URISyntaxException, IOException {
// checking for mandatory application argument
if (args.length != 1) {
System.out.println("Usage:\njava -jar JarFileCopyTester.jar <targetFolderPath>");
System.exit(0);
}
// the destination folder path, without filename
String destinationFolderPath = args[0];
if (!destinationFolderPath.endsWith(File.separator)) {
destinationFolderPath = destinationFolderPath + File.separator;
}
// getting the running jar file path
String jarFilePath = JarFileCopyTester.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();
// getting Path object related to the jarFilePath
Path sourcePath = (new File(jarFilePath)).toPath();
System.out.println("sourcePath: " + sourcePath);
// getting jar file name only, to compose dest file path
Path jarFileNameOnly = sourcePath.getFileName();
// adding filename to the destination folder path
String destinationFilePath = destinationFolderPath + jarFileNameOnly.toString();
// composing the Path object for the destination file path
Path destPath =(new File(destinationFilePath)).toPath();
System.out.println("destination path: " + destPath);
// copying the file
Files.copy(sourcePath, destPath, REPLACE_EXISTING);
}
}
die JAR-Datei ausführen, rufen Sie den Befehl:
Fenster:
java -jar JarFileCopyTester.jar c:\myFolder1\myFolder2
Linux:
java -jar JarFileCopyTester.jar /home/username/
Einige Beiträge Verweisen Sie, dass 'toURI()' Probleme verursachen kann, wenn es gibt nicht englische Zeichen, die in den Pfad involviert sind. Wie kann ich mit dieser Situation umgehen? – FiroKun
@FiroKun nur mit einem Ordner mit russischen Namen überprüft, funktioniert der Code in Ordnung (Ubuntu Linux, Java 1.8.0_77). Hast du Probleme mit diesem Code? –
@FiroKun überprüfen Sie dieses Bild: http://i.imgur.com/5EzFQNm.jpg Ich habe einen Ordner 'тестовая папка' (bedeutet' Testordner') erstellt und diesen Code in diesem Ordner ausgeführt. Alles hat gut funktioniert. –