[英]How get absolute path to <project>\taget folder
I need to get an absolute path to <project>/target
folder.我需要获得
<project>/target
文件夹的绝对路径。 I use the following code:我使用以下代码:
Main main = new Main();
String testPath = main.getClass().getResource("").getPath();
System.out.println(testPath);
It returns the path like /E:/java/main/target/classes/
But I need to get /E:/java/main/target/
它返回像
/E:/java/main/target/classes/
这样的路径但我需要得到/E:/java/main/target/
How should I set a value in getResource()
?我应该如何在
getResource()
设置一个值?
In my solution I can not use System.getProperty("user.dir");
在我的解决方案中,我不能使用
System.getProperty("user.dir");
for some reason.因为某些原因。
The following line will locate you to the base directory, where are the src
, resources
and target
folders located:以下行会将您定位到基本目录,其中
src
、 resources
和target
文件夹位于:
System.getProperty("user.dir");
It is the directory, where the JVM was started and those properties are described at System Properties .它是启动 JVM 的目录,这些属性在System Properties 中进行了描述。 The full search would look like:
完整的搜索看起来像:
final String targetPath = System.getProperty("user.dir");
final File target = Arrays.stream(new File(targetPath).listFiles(File::isDirectory))
.filter(file -> "target".equals(file.getName()))
.findAny()
.orElseThrow(() -> new FileNotFoundException("The target has not been found"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.