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

Java reading file content
Trying to get includes working with AsciiDoc Java Interface
Java the best way of waiting & getting data from your client
JMeter Custom Java Client to Test Websocket Application
Using OR statment in condition sqlite database
Controll Java Swing Application over Bluetooth
Java logger better formatting
Limiting duplicate random numbers
How to fetch a <map> in Hibernate
Yaroslavskiy’s dual pivot quick sort algorithm
Checking the clipboard contents
Fetch Array of key values from Mongo collection using java
Passing cmd line arguments to specific method [duplicate]
error when use onClickListener in listActivity from SQLite DB
ObjectAnimator Choppy Scrolling
Manipulating a colored image in matrix form in java

Categories

HOME
maven
clips
tinymce
homebrew
relayjs
yahoo-oauth
maven-3
cmd
amazon-ecs
v8
retrofit
baqend
blueprintjs
ojdbc
azure-media-services
django-imagekit
ios-charts
google-translate
remote-access
task
clojurescript
propel
evopdf
custom-wordpress-pages
nas
scriptcs
jquery-ajaxq
functional-testing
trading
sqlcipher
spark-jobserver
chromebook
entitlements
publish
srcset
nouislider
bluestacks
javascriptcore
key-value-observing
stringtemplate
commit
openoffice.org
mpmediaquery
dartium
hot-module-replacement
dynamics-crm-2013
angularjs-factory
mapzen
avro4s
reportingservices-2005
android-mediaprojection
executenonquery
osx-mavericks
smartcontracts
apache-fop
return-value
integrity
tropo
aurelia-validation
google-perftools
watchconnectivity
nested-sets
setuptools
deadbolt-2
google-feed-api
pcf
xml-attribute
sdf
nessus
design-by-contract
two-factor-authentication
dia
sgen
navigationservice
marmalade
ember-charts
algebraic-data-types
jplaton
jquery-mobile-dialog
plasma
orchardcms-1.7
distutils
e4x
trusted
bluepill
cascalog
hosts-file
appconkit
regsvr32
osql
custom-backend
vdsp
heartbeat
coredump
w3wp.exe
createwindow
sortable-tables
sproutcore-2
vc90
compiler-specific
firefox4
zend-tool
geneva-server

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