53

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.

zb226
  • 9,586
  • 6
  • 49
  • 79
Jay Smith
  • 2,331
  • 3
  • 16
  • 27
  • 2
    This doesn't work because your classpath is composed of transparent and **opaque** resources - such as those inside a `jar`. You are trying to write to a path that looks something like `jar:file:/com/stackoverflow/json`, which is an invalid `Path` or `File` but a valid URI. In general, you cannot write to the classpath, only read from it. – Boris the Spider May 15 '17 at 06:47
  • No jar it is maven project – Jay Smith May 15 '17 at 06:49
  • When you compile a Maven project it will generate a jar. How else would you distribute your compiled code? (Pre Java 9 that is) – Boris the Spider May 15 '17 at 06:50
  • Does this answer your question? [Java NIO file path issue](https://stackoverflow.com/questions/9834776/java-nio-file-path-issue) – Pino Nov 14 '19 at 11:00

8 Answers8

67

problem is that Paths.get() doesn't 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");
Dmitriy Popov
  • 2,150
  • 3
  • 25
  • 34
hunter
  • 3,963
  • 1
  • 16
  • 19
10

Try this:

Path path = new File(getClass()
.getResource("/<path to the image in your build/classes folder>")
.getFile()).toPath();

to get the correct path. Worked for me after several hours trying to find out why I couldn't get the file from the jar. This works for NetBeans 8.02

lczapski
  • 4,026
  • 3
  • 16
  • 32
Fego
  • 145
  • 1
  • 7
5

I had the same issue and got the exception, noticed there was a space in the filename, so I had to trim it. After that, the issue is resolved.

Path filePath = Paths.get(dirPathStr, newFileName.trim());
svarog
  • 9,477
  • 4
  • 61
  • 77
techguy
  • 61
  • 1
  • 2
3

After trying many times, this worked for me

      Path path = new File(getClass().getResource("/data.json").getFile()).toPath(); 

Now you can use your path as you wish e.g

        Reader reader = Files.newBufferedReader(path);
Odwori
  • 1,460
  • 13
  • 14
2

I have the same problem which I was facing from the past two days and finally, I got it Space causes such problem try to solve

var fileName=YourFileName.trim();
Path filePath = Paths.get(dirPathStr, fileName);
tryingToLearn
  • 10,691
  • 12
  • 80
  • 114
1

I had the same error

./gradlew build

2023-04-10T16:56:54.855+02:00 INFO 10476 --- [ main] c.v.s.d.m.a.VogellaApplication : No active profile set, falling back to 1 default profile: "default" Exception in thread "main" java.nio.file.InvalidPathException: Trailing char < > at index 27: com.vogella.spring.di.model at java.base/sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:191) at java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153) at java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77) at java.base/sun.nio.fs.WindowsPath.parse(WindowsPath.java:92)

I analyzed the filenames and the configuration. I noticed a space in the 'group' property in build.gradle file

group = 'com.vogella.spring.di.model '

I removed the extra space and the build command worked fine.

0

The following solutions work correctly:

Solution1:

String logFileLocalPath = "../log/log.txt";
File logFile = new File(getURL(logFileLocalPath).getPath());

Solution 2:

File logFile = new 
File(Paths.get(getURL(logFileLocalPath).toURI()).toString());

private static URL getURL(String localPath) {

      return Main.class.getResource(localPath);
}
Osadhi Virochana
  • 1,294
  • 2
  • 11
  • 21
Programmer
  • 57
  • 7
0

I faced same issue while using extentReports in my automation, i simply corrected the report path

public void config() {
    String path = System.getProperty("user.dir")+"\\reports\\index.html";       
    ExtentSparkReporter reporter = new ExtentSparkReporter(path);
    reporter.config().setReportName("Web Automation Reports");
    reporter.config().setDocumentTitle("Web Results");

... }