简体   繁体   中英

How do I copy a file from src/main/resources in Maven?

imgur

Hi, I want to copy "play button.png" to "C:\\Users\\Wyatt\\AppData\\Roaming\\.The Labyrinth\\Assets\\Images". I tried useing this code:

File appdata = new File(System.getenv("APPDATA"));
File datafolder = new File(appdata, ".The Labyrinth");

File assets = new File(datafolder, "Assets");
assets.mkdir();

Files.copy(Paths.get("src\\main\\resources\\assets\\play button.png"), Paths.get(assets + "\\Images\\play button.png"));

But it throws an exception.

java.nio.file.NoSuchFileException: src\main\resources\assets\play button.png -> C:\Users\Wyatt\AppData\Roaming\.The Labyrinth\Assets\Images\play button.png
at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsFileCopy.copy(Unknown Source)
at sun.nio.fs.WindowsFileSystemProvider.copy(Unknown Source)
at java.nio.file.Files.copy(Unknown Source)
at com.awsp8.labyrinth.TheLabyrinth.install(TheLabyrinth.java:73)
at com.awsp8.labyrinth.TheLabyrinth.main(TheLabyrinth.java:32)

TheLabyrinth.java:73 is this code:

Files.copy(Paths.get("src\\main\\resources\\assets\\play button.png"), Paths.get(assets + "\\Images\\play button.png"));

Maybe I'm doing this wrong? I dont know. Thanks in advance.

Not sure what are you trying to do, but:

1) In your java code use / instead of \\ , it will work also on windows and it is better to maintain.

2) Your src/main/resources/assets/play button.png is relative to destination where you run your code and I doubt that C:\\Users\\Wyatt\\AppData\\Roaming.The Labyrinth\\Assets\\Images\\ is the directory you want to point.

3) If you have your play button.png (terrible name, I hate names with spaces :-)) in \\main\\reources and use maven standard layout you probably have it on your classpath, don' t you ? If so, why not use something like ClassLoader.getSystemClassLoader().getResourceAsStream("play button.png") to obtain InputStream to read from ?

4) if you really want to copy something via Maven (which your question caption is about, but i am pretty sure your question not) this is the sample how to do it:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>${maven.antrun.plugin.version}</version>
    <executions>
        <execution>
            <id>copy jars</id>
            <phase>install</phase>
            <configuration>
                <target>
                    <copy file="${project.basedir}/src/main/resources/play button.png"
                                tofile="somewhere i want to copy.png" />
                </target>
                </configuration>
                <goals>
                    <goal>run</goal>
                </goals>
        </execution>
    </executions>
</plugin>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM