[英]How do I copy a file from src/main/resources in Maven?
Hi, I want to copy "play button.png" to "C:\\Users\\Wyatt\\AppData\\Roaming\\.The Labyrinth\\Assets\\Images". 嗨,我想将“播放button.png”复制到“ 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: TheLabyrinth.java:73是以下代码:
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. 1)在您的Java代码中,使用/代替\\,它也可以在Windows上运行,并且最好进行维护。
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. 2)您的src / main / resources / assets / play button.png相对于您运行代码的目标位置,我怀疑C:\\ Users \\ Wyatt \\ AppData \\ Roaming.Labyrinth \\ Assets \\ Images \\是您的目录要点。
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 ? 3)如果在\\ main \\ reources中有您的play button.png(糟糕的名字,我讨厌带有空格的名称:-))并使用Maven标准布局,则可能在类路径中有它,不是吗? If so, why not use something like ClassLoader.getSystemClassLoader().getResourceAsStream("play button.png") to obtain InputStream to read from ? 如果是这样,为什么不使用类似ClassLoader.getSystemClassLoader()。getResourceAsStream(“ play button.png”)之类的方法来获取要从中读取的InputStream呢?
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: 4)如果您真的想通过Maven复制某些内容(您的问题标题与之有关,但是我很确定您的问题不是),这是示例操作方法:
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.