You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

带表头CSV拆分Java代码在Java 22环境编译报错求助

解决CSV拆分Java代码的Apache POI编译错误

问题背景

我编写了一段按固定行数拆分带表头CSV文件的Java代码,此前在Windows 7 + Java 1.8.0_202环境下可正常编译运行。更换到Windows 11 + Java 22.0.1环境后,使用javac命令编译时,出现Apache POI相关包不存在的错误。

代码内容

import java.io.*;  
import java.util.Scanner;

import java.io.File;  
import java.io.FileInputStream;  
import java.util.Iterator;  
import org.apache.poi.ss.usermodel.Cell;  
import org.apache.poi.ss.usermodel.Row;  
import org.apache.poi.xssf.usermodel.XSSFSheet;  
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


public class csv_split_with_hdr {  
public static void main(String args[])  {  
 try{  
  String inputfile = "C:/SUMIT/backup-old/SFDC/policy-extract/temp1/xxx.csv";
  System.out.println("Input Path is :- "+  inputfile);
  double nol = 2800.0; 
  File file = new File(inputfile);  
  Scanner scanner = new Scanner(file);  
  int count = 0;  
      while (scanner.hasNextLine())   
       {  
        scanner.nextLine();  
        count++;  
       }  
      System.out.println("Lines in the file: " + count);     

       double temp = (count/nol);  
       int temp1=(int)temp;  
       int nof=0;  
         if(temp1==temp)  
          {  
           nof=temp1;  
          }  
         else  
          {  
           nof=temp1+1;  
          }  
      System.out.println("No. of files to be generated :"+nof); 

  FileInputStream fstream = new FileInputStream(inputfile); DataInputStream in = new DataInputStream(fstream);  
  BufferedReader br = new BufferedReader(new InputStreamReader(in));
  String strLine;
  String header;
  header = br.readLine();

        for (int j=1;j<=nof;j++)  
         {  
          String outputpath = "C:/SUMIT/backup-old/SFDC/policy-extract/temp1";
          String outputfile = "xxx"+j+".csv";     
          System.out.println(outputpath+outputfile);
          FileWriter fstream1 = new FileWriter(outputpath+outputfile);     

         BufferedWriter out = new BufferedWriter(fstream1);
         out.write(header);
         out.newLine();
         for (int i=1;i<=nol;i++)  
          {  
           strLine = br.readLine();   
           if (strLine!= null)  
             {  
              out.write(strLine);   
              if(i!=nol)  
               {  
                out.newLine();  
               }  
             }  
          }  
          out.close();  
         }  

         in.close();  
    }
 catch (Exception e)  
 {  
  System.err.println("Error: " + e.getMessage());  
 }  
}  
}

原运行环境

Processor : intel CORE i3, OS :- windows 7
java version :- "1.8.0_202"
java(TM) SE RUNTIME Environment (build 1.8.0_202-b08)
java Hotspot(TM) client VM (build 25.202-b08, mixed mode)

当前运行环境

Processor :  intel CORE 13, OS :- Windows 11
java version :- "22.0.1"  2024-04-16
java(TM) SE RUNTIME Environment (build 22.0.1+8-16)
java Hotspot(TM) 64-Bit Server VM (build 22.0.1+8-16, mixed mode, sharing)

编译错误信息

C:\sumit\backup-old\SFDC\policy-extract\temp1>javac csv_split_with_hdr.java
csv_split_with_hdr.java:7: error: package org.apache.poi.ss.usermodel does not exist import org.apache.poi.ss.usermodel.Cell;
                                  ^
csv_split_with_hdr.java:8: error: package org.apache.poi.ss.usermodel does not exist import org.apache.poi.ss.usermodel.Row;
                                  ^
csv_split_with_hdr.java:9: error: package org.apache.poi.xssf.usermodel does not exist import org.apache.poi.xssf.usermodel.XSSFSheet;
                                ^
csv_split_with_hdr.java:10: error: package org.apache.poi.xssf.usermodel does not exist import org.apache.poi.xssf.usermodel.XSSFWorkbook;
                                ^
                              
4 errors
C:\sumit\backup-old\SFDC\policy-extract\temp1>

解决方案

1. 移除无用的POI导入(快速解决)

仔细查看代码可以发现,导入的org.apache.poi相关类(Cell、Row、XSSFSheet、XSSFWorkbook)在代码中完全没有被使用,属于冗余导入。直接删除这些语句,代码即可正常编译。

修改后的代码开头:

import java.io.*;  
import java.util.Scanner;

import java.io.File;  
import java.io.FileInputStream;  
import java.util.Iterator;  

2. 若后续需使用POI功能(可选)

如果之后要扩展代码处理Excel文件,需要:

  • 下载兼容Java 22的Apache POI包(推荐POI 5.2.5及以上版本)
  • 编译时通过-cp参数指定jar包路径,示例命令:
    javac -cp "poi-5.2.5.jar;poi-ooxml-5.2.5.jar;poi-ooxml-lite-5.2.5.jar;commons-codec-1.15.jar;commons-collections4-4.4.jar;commons-math3-3.6.1.jar;log4j-api-2.20.0.jar" csv_split_with_hdr.java
    
  • 运行时同样需要指定类路径:
    java -cp ".;poi-5.2.5.jar;poi-ooxml-5.2.5.jar;poi-ooxml-lite-5.2.5.jar;commons-codec-1.15.jar;commons-collections4-4.4.jar;commons-math3-3.6.1.jar;log4j-api-2.20.0.jar" csv_split_with_hdr
    

内容的提问来源于stack exchange,提问作者user7616778

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 09:17:32