使用命令行运行maven项目时出现“java.io.FileNotFoundException: (系统找不到指定的路径)”错误

问题描述 投票:0回答:1

在下面的代码中,我使用apache poi来读取excel文件。该代码在 Eclipse 中运行良好。但是当我使用maven命令运行程序时,它显示以下错误。

commands used in cmd

package Question6;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.*;
import java.util.*;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ReadAnWriteDate { 
    
    public static void main(String[] args)  {
        
        ArrayList<List> a=new ArrayList<List>();
        a=read_data ("src/main/resources/input_excel/StudentDetails.xlsx", "Sheet1"); 
        print_data(a);  
    }
    

    public static ArrayList<List> read_data(String workbookPath, String sheetName){
        
        ArrayList<List> testdata = new ArrayList<List>();
        
        try
        {
            FileInputStream inputStream = new FileInputStream(new File(workbookPath));
            
            XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
            XSSFSheet sheet = workbook.getSheet(sheetName); 
            
            //FormulaEvaluator formulaEvaluator = workbook.getCreationHelper().createFormulaEvaluator();
            
            int rowCount=sheet.getLastRowNum();
            int columnCount=sheet.getRow(0).getLastCellNum();
            
            
            for(int i=1; i<=rowCount; i++) {
                List <Object> rowData=new ArrayList<Object>();
                XSSFRow row = sheet.getRow(i);
                
                for(int j=0; j<columnCount;j++) {
                    
                    XSSFCell cell=row.getCell(j);
                    switch(cell.getCellType()) {
                    case STRING:
                        rowData.add(cell);
                        break; 
                    case NUMERIC: 
                        rowData.add(cell);
                        break;                      
                    }
                }   

                testdata.add(rowData);
            }   
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        
        return testdata;
        
    }
    
    
    
    public static void print_data(ArrayList<List> data)
    {
        for(int i=0;i<data.size(); i++) {
            List<Object> x = new ArrayList<>();
            x=data.get(i);
            
            for (int j=0;j<x.size(); j++){
                System.out.println(x.get(j));
            }
            System.out.println();
        }
    }

}



文件夹结构: folder structure image

错误: error image

我尝试在所有文件夹中添加 Excel 填充,并在编码中仅给出文件名。但没有运气

maven filenotfoundexception
1个回答
0
投票

将您的资源添加为:

 <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>*/**</include>
            </includes>
        </resource>
    </resources>

并将路径更改为相对路径: a=read_data("input_excel/StudentDetails.xlsx","Sheet1"); 应该没问题

© www.soinside.com 2019 - 2024. All rights reserved.