嗨,我從jar文件運行java應用程序。如下面的java -cp test.jar com.test.TestMain
。在java應用程序中,我正在閱讀csv文件。這是拋出異常。java.io.FileNotFoundException從jar文件運行java應用程序
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())) {
感謝。
你能分享更多來自TestMain.java的代碼請 – nullpointer
這兩行是什麼'at com.test.TestMain.run(TestMain.java:63) at com.test.TestMain.main(TestMain.java: 43)'你們班上? – nullpointer
將'src/main/resources'放入類路徑 – Sanjeev