java


Exception in thread “main” java.nio.file.InvalidPathException: Illegal char <:> at index 2:


I have to copy classpath resource from one package to another.
My program is:
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);
}
At Files.copy method I get 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)
How to solve it?
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);
}
This code correctly copies Movie.class from package com/stackoverflow/main into com/stackoverflow/json.

problem is that Paths.get() doesnt expect that kind of value which is generated from uri.getPath().
Solution:
URI uri = ClassLoader.getSystemResource("com/stackoverflow/json").toURI();
String mainPath = Paths.get(uri).toString();
Path path = Paths.get(mainPath ,"Movie.class");

Try to change your / with //. This will resolve the problem.
"com/stackoverflow/main/Movie.class" to "com//stackoverflow//main//Movie.class"
"com/stackoverflow/json" to "com//stackoverflow//json" and so on.


Related Links

Declaring a static variable in a non-static class in Java
How can I mock the return from a generic wildcard
Pair autoscaled app & db servers together with consul
Backtracking is not working for printing all combinations of a string?
Why the method generateCertificate return a CertificateException when I trying to add my SSL certificate?
Send message to a specific socket id from android/ java client using Socket.IO java client library
How to use hibernate criteria with calculation values?
Undertanding Regex Pattern matching in Java [duplicate]
java.lang.String cannot be cast to [Ljava.lang.Object while Iterating For Loop
Need Help to get librxtxSerial.jnilib into runnable jar file
Java pdf to Excel Conversion
Not able to call shell script from procedure using java source
Compare contents of two ArrayLists efficiently
java.sql.Timestamp#valueOf result depends on default timezone [duplicate]
Error 'Error:Execution failed for task' when I Generate Singed App
How to properly implements Hibernate from an already existing database?

Categories

HOME
reflection
onedrive
objectgears
at-command
react-redux
youtube-api-v3
c#-2.0
binary-tree
echarts
bs4
basic
midi
dtrace
flask-wtforms
netflix
seaborn
django-admin
transformation
ef-migrations
apache-metamodel
evopdf
excel-vba-mac
samoa
jprofiler
kryo
ehcache
graphicsmagick
sylius
microsoft-r
crosstab
semantic-analysis
intel-pin
greendao
minitab
opentype
windows-server-2000
webkitspeechrecognition
vision
gsmcomm
librato
yadcf
noraui
ghost4j
mmenu
environment-modules
catalog
web-mining
multilingual
sharefile
trim
grid.mvc
http-live-streaming
estimote
qwt
user-accounts
rainbowtable
qsslsocket
multipeer-connectivity
libusb-win32
google-cdn
messenger
ableton-live
google-feed-api
angular-cache
fody
login-control
measures
gstreamer-0.10
testng-dataprovider
fadeout
asp.net5
netmq
execute
sdhc
eclipse-clp
jmeter-maven-plugin
django-unittest
sniffer
android-nested-fragment
sgen
ms-project-server-2010
dataservice
sqlperformance
sabredav
angularjs-controller
quickdialog
multipage
hosts-file
getmessage
custom-backend
infobox
xmemcached
horizontal-accordion
android-sdk-2.1
w3wp.exe
web-application-design
infrastructure
dentrix





Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm