英文:
Java location path regex split
问题
I've "inherited" existing Java Selenium & Cucumber framework which was written mostly for OS usage. I'm using Windows and I'm trying to fix & run it on Windows.
My first problem is specifying the correct file path, this is how it was written for OS:
private String getProjectName(Scenario scenario) {
return Arrays.asList(scenario.getUri().getPath().replace(System.getProperty("user.dir"), "").split("/")).get(5);
}
Error which I'm receiving is:
java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 1
As for Windows, we're using backslashes. I've tried switching "/" into "", but as the error appears (+ after my investigations), I've tried with "\" but actually, the error remains the same as above.
I'm aware that providing only a portion of my code, and it may be hard, but for the first glance, can you tell me:
- If that method may work on Windows or this should be completely refactored?
- Is
System.getProperty("user.dir")
the correct solution? - How to correctly pass backslashes?
- Why they're using
.get(5)
?
英文:
I've "inherited" existing Java Selenium & Cucumber framework which was written mostly for OS usage. I'm using Windows and I'm trying to fix & run it on Windows.
My first problem is specifing corrent file path, this is how it was written for OS:
private String getProjectName(Scenario scenario) {
return Arrays.asList(scenario.getUri().getPath().replace(System.getProperty("user.dir"), "").split("/")).get(5);
}
Error which I'm receiving is:
java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 1
As for Windows we're using backlashes I've tried switching "/" into "" but as error appears (+ after my investigations) I've tried with "\\\\" but actually error remains the same as above.
I'm aware that providing only portion of my code and it may be hard but for the first glance can you tell me:
- If that method may work on Windows or this should be completely refactored?
- Is
System.getProperty("user.dir")
correct solution? - How to correctly pass backslashes?
- Why they're taking
.get(5)
?
答案1
得分: 3
我可以猜测:
这种方法获取的是项目名称,很可能是场景文件所在的文件夹结构中的某个文件夹的名称。
这就是为什么他们选择第5
个元素。因为在第5
级别上有代表项目的文件夹。
所使用的方法看起来相当有争议。至少因为有一些多余的步骤,比如转换为列表。
现在,你应该怎么做:
正确的方式是使用java.nio.file.Path
(从Java 7开始),它会处理不同操作系统特定的事情。
所以你的代码可能如下所示:
private String getProjectName(Scenario scenario) {
return Path.of(scenario.getUri()).getName(5);
}
附言: - 当然,你需要将5
更改为捕获所需文件夹在你的结构中的正确位置。
英文:
I can guess:
This method is taking the project name that is likely the name of a certain folder in the folder structure where scenario file located.
This is why they took 5
th element. Because on the 5
th level there was the folder which represented the project.
The used approach look very arguable. At least because there are some redundent steps like converting to list.
Now. How would you go:
The proper way is to use java.nio.file.Path
(starts from Java 7) that takes care of differnt OS-specific things.
So your code might look like:
private String getProjectName(Scenario scenario) {
return Path.of(scenario.getUri()).getName(5)
}
P.S. - of course you have to change 5
to catch a proper position of the required folder in your structure.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论