gpt4 book ai didi

从 jar 文件运行 java 应用程序时出现 java.io.FileNotFoundException

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:41:21 25 4
gpt4 key购买 nike

您好,我正在从 jar 文件运行 java 应用程序。就像下面的 java -cp test.jar com.test.TestMain 。在 java 应用程序中,我正在读取 csv 文件。抛出以下异常。

java.io.FileNotFoundException: file:\C:\Users\harinath.BBI0\Desktop\test.jar!\us_postal_codes.csv (The filename, directory name, or volume label syntax is incorrect)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:146)
at java.util.Scanner.<init>(Scanner.java:656)
at com.test.TestMain.run(TestMain.java:63)
at com.test.TestMain.main(TestMain.java:43)

*csv file is located in src/main/resources folder.

导致异常的代码是

public static void main(String[] args) throws Exception {
TestMain trainerScraper = new TestMain();
trainerScraper.run();
}

private void run() throws JsonParseException, JsonMappingException, IOException{
String line = "";
String cvsSplitBy = ",";

//Get file from resources folder
ClassLoader classLoader = getClass().getClassLoader();
System.out.println(csvFile);
URL url = classLoader.getResource("us_postal_codes.csv");
String fileName = url.getFile();
File file = new File(fileName);

try (Scanner scanner = new Scanner(file)) {
line = scanner.nextLine(); //header
while ((scanner.hasNextLine())) {

谢谢。

最佳答案

test.jar!\us_postal_codes.csv (The filename, directory name, or volume label syntax is incorrect)

建议使用

System.getProperty("user.dir") // to get the current directory, if the resource is in the project folder

getResourceAsStream("/us_postal_codes.csv") // if its inside a jar

关于从 jar 文件运行 java 应用程序时出现 java.io.FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37270047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com