Exception in thread “main” java.nio.fichier.InvalidPathException: Illégal char <:> à l'index 2:
J'ai copier classpath ressource à partir d'un package à un autre.
Mon programme est:
public static void main(String[] args) throws IOException, URISyntaxException {
ClassLoader classLoader = CopyFileToDirectoryTest.class.getClassLoader();
InputStream in = classLoader.getResourceAsStream("com/stackoverflow/main/Movie.class");
URI uri = ClassLoader.getSystemResource("com/stackoverflow/json").toURI();
Path path = Paths.get(uri.getPath(),"Movie.class");
System.out.println(path);
long copy = Files.copy(in, path, StandardCopyOption.REPLACE_EXISTING);
System.out.println(copy);
}
À Files.copy
méthode-je obtenir de l'exception:
Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <:> at index 2: /D:/Programs/workspaceEE/HibernateDemo/target/classes/com/stackoverflow/json
at sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182)
at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153)
at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77)
at sun.nio.fs.WindowsPath.parse(WindowsPath.java:94)
at sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:255)
at java.nio.file.Paths.get(Paths.java:84)
at com.stackoverflow.main.CopyFileToDirectoryTest.main(CopyFileToDirectoryTest.java:34)
Comment le résoudre?
Solution
public static void main(String[] args) throws IOException, URISyntaxException {
ClassLoader classLoader = CopyFileToDirectoryTest.class.getClassLoader();
InputStream in = classLoader.getResourceAsStream("com//stackoverflow//main//Movie.class");
URI uri = ClassLoader.getSystemResource("com//stackoverflow//json").toURI();
String mainPath = Paths.get(uri).toString();
Path path = Paths.get(mainPath, "Movie.class");
System.out.println(path);
long copy = Files.copy(in, path, StandardCopyOption.REPLACE_EXISTING);
System.out.println(copy);
}
Correctement ce code copies Movie.class
de colis com/stackoverflow/main
en com/stackoverflow/json
.
Cela ne fonctionne pas parce que votre classe est composée de transparence et de opaque ressources - telles que celles à l'intérieur d'un
Pas de pot, il est projet maven
Lorsque vous compilez un projet Maven, il va générer un jar. Sinon, comment voulez-vous distribuer votre code compilé? (Pré Java 9)
jar
. Vous essayez d'écrire sur un chemin qui ressemble à quelque chose comme jar:file:/com/stackoverflow/json
, ce qui n'est pas valide Path
ou File
mais une URI valide. En général, vous ne pouvez pas écrire le chemin de la classe, seulement les lire.Pas de pot, il est projet maven
Lorsque vous compilez un projet Maven, il va générer un jar. Sinon, comment voulez-vous distribuer votre code compilé? (Pré Java 9)
OriginalL'auteur Jay Smith | 2017-05-15
Vous devez vous connecter pour publier un commentaire.
problème est que
Paths.get()
ne pas s'attendre à ce que le type de valeur qui est généré à partir deuri.getPath()
.Solution:
OriginalL'auteur hunter
J'ai eu le même problème et a obtenu l'exception, remarqué qu'il y avait un espace dans le nom de fichier, donc j'ai dû couper. Après cela, le problème est résolu.
OriginalL'auteur techguy
Essayez de changer votre
/
avec//
. Cela permettra de résoudre le problème."com/stackoverflow/main/Movie.class"
à"com//stackoverflow//main//Movie.class"
"com/stackoverflow/json"
à"com//stackoverflow//json"
et ainsi de suite./D:/Programs/workspaceEE/HibernateDemo/target/classes/com//stackoverflow//json\Movie.class
. Mais j'ai explicitement supprimées en premier/
et remplacé tous les/
avec//
. Et passé ce que l'argument dePaths.get
et ça marche!!!Pourquoi en serait-il fait aucune différence. Je suppose que vous êtes en train de penser à propos de windows `\` séparateurs de chemin.
Je suis sur windows
Un grand problème est résolu 🙂
OriginalL'auteur Mehraj Malik