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

WARNING: No mapping found for HTTP request with URI [/SpringTest/add] in DispatcherServlet with name telusuko
Why the JUnit is calling “object” and not “exception”?
Automatic horizontally scrolling textview getting initialized everytime an image is changing in runnable
Dependency of one class on three interfaces Guice
How to go about creating a user authentication system for a Java web app on AWS Elastic Beanstalk?
Gradle sync failed: Cause: error in opening zip file Corrupt Dependency Cache
Is using “final” parameter thread safe in Java
Android Studio Automatically Erases Extra Spaces In txt Files
Android Studio XML to Java to XML Conversions
Clojure JUNG setVertexFillPaintTransformer
I cant connect SQL Server ClassNotFoundException
Java String to Date on POST Method
Collecting lists from an object list using Java 8 Stream API
Java single thread of execution and cpu cache
How to find list of gcd of an array, while neglecting 0 to nth element(find gcd of (n-1) elements) one by one?
How to get list of Sheet's via Google V4 Java API

Categories

HOME
vim
hive
cloud
vbscript
keras
reflection
grep
rsyslog
razor
react-redux
spring-jdbc
cakephp-2.5
ckeditor
angular-ui-bootstrap
django-imagekit
session-variables
communication
metatrader4
offline
google-cloud-spanner
timeout
usergrid
dtrace
worldwind
aurigma
reactcsstransitiongroup
sms-gateway
dbext
amazonsellercentral
one-hot-encoding
clickonce
crystal-reports-2010
numerical-methods
spark-jobserver
virtualdub
large-file-upload
bcd
catel
commit
y86
revolution-slider
bitbucket-pipelines
x11-forwarding
ssjs
leading-zero
temporary-files
optix
botbuilder
mime
total-commander
qcombobox
osx-mavericks
parentheses
youcompleteme
react-scripts
rdfs
dtexec
lync-client-sdk
or-tools
log4c
azure-virtual-network
url-masking
mikroc
actionbardrawertoggle
hill-climbing
ios4
energy
maven-tomcat-plugin
intel-fortran
mutation-observers
purge
jqgrid-formatter
angular-leaflet-directive
createprocessasuser
skos
csquery
block-device
tween
rdtsc
wordpress-theme-customize
fluentautomation
valuechangelistener
concurrent-collections
srs
centos5
cdc
jmapviewer
ftps
ril
interface-orientation
sharp-repository
clipper
chuck
first-responder
parametric-equations
wse3.0
gwt-ext
window-management
gnustep
horizontal-accordion
yetanotherforum
lpeg
webkit.net
privilege
project-hosting
port-number

Resources

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
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App