1

This is my code:

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

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 Reader {

    public static void read_excel() {

        File excel =  new File ("C:\\Users\\Username\\Desktop\\java-Tools\\data.xlsx");
        FileInputStream fis = new FileInputStream(excel);
        XSSFWorkbook wb = new XSSFWorkbook(fis);
}

This results in the following error message:

error: cannot find symbol 
File excel =  new File ("C:\\Users\\Username\\Desktop\\java-Tools\\data.xlsx");
symbol: class File
location: class reader

I have set the CLASSPATH for the jar files of the Apache POI library. Here is the content of the CLASSPATH varibale:

.;C:\Users\Username\Desktop\Code\Classes;C:\poi-3.12\poi-3.12-20150511.jar;C:\poi-3.12\poi-ooxml-3.12-20150511.jar;C:\poi-3.12\poi-ooxml-schemas-3.12-20150511.jar;C:\poi-3.12\ooxml-lib\xmlbeans-2.6.0.jar;C:\poi-3.12\lib\commons-codec-1.9.jar;C:\poi-3.12\lib\commons-logging-1.1.3.jar;C:\poi-3.12\lib\junit-4.12.jar; C:\poi-3.12\lib\log4j-1.2.17.jar;C:\poi-3.12\poi-examples-3.12-20150511.jar;C:\poi-3.12\poi-excelant-3.12-20150511.jar;C:\poi-3.12\poi-scratchpad-3.12-20150511.jar

I don't understand why the programme does not compile !

steady_progress
  • 3,311
  • 10
  • 31
  • 62
  • 1
    Please read this: http://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-compilation-error-mean. It explains the various reasons you can get a "Cannot find symbol" compilation error ... and why your program doesn't compile. – Stephen C Sep 08 '15 at 11:58

3 Answers3

1

Add import statement for File

import java.io.File;
Rahul Yadav
  • 1,503
  • 8
  • 11
  • Thank you very much fpr your quick answer. I did as you told me and compiled the file again ... now I get the following error message: "error: unreported exception FileNotFoundException; must be caught or declared to be thrown FileInputStream fis = new FileInputStream(excel); error: unreported exception IOException; must be caught or declared to be thrown XSSFWorkbook wb = new XSSFWorkbook(fis); 2 errors" – steady_progress Sep 08 '15 at 11:35
  • 1
    Add try catchBlock and handle FileNotFoundException or add throws declaration to you method signature. I would suggest you to use some IDEs in case you're not using any. – Rahul Yadav Sep 08 '15 at 11:36
  • but why is the file not found ? ... the path to the file is correect. – steady_progress Sep 08 '15 at 11:40
  • You are still not getting any exception, its a compile time exception wherein you will need to handle any checked exception using try catch or throws keyword. For further info please read java api docs. – Rahul Yadav Sep 08 '15 at 11:44
  • 2
    Compile time ERROR ... not exception. (Please lets not confuse the beginner even further by using incorrect terminology.) – Stephen C Sep 08 '15 at 12:25
1

try with the following code

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

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 Reader {

    public static void read_excel() throws FileNotFoundException {

        File excel =  new File ("C:\\Users\\Username\\Desktop\\java-Tools\\data.xlsx");
        FileInputStream fis = new FileInputStream(excel);
        XSSFWorkbook wb = new XSSFWorkbook(fis);
}
SpringLearner
  • 13,738
  • 20
  • 78
  • 116
  • Thank you for your answer ... with your code I get the following error message: "error: cannot find symbol public static void read_excel() throws FileNotFoundException { symbol: class FileNotFoundException location: class Reader 1 error" – steady_progress Sep 08 '15 at 12:11
  • @steady_progress did you copy exactly all my code if yes then you need to add } – SpringLearner Sep 08 '15 at 12:32
  • The [Apache POI docs are pretty clear](http://poi.apache.org/spreadsheet/quick-guide.html#FileInputStream) on not using an `InputStream` if you have a `File`! – Gagravarr Sep 08 '15 at 13:24
1

Ok, this code here finally did not produce an error message:

import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.File;
import java.util.*;

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 Reader {

    public static void read_excel()  throws FileNotFoundException, IOException {

        File excel =  new File ("C:\\Users\\Username\\Desktop\\java-Tools\\data.xlsx");
        FileInputStream fis = new FileInputStream(excel);
        XSSFWorkbook wb = new XSSFWorkbook(fis);
}
steady_progress
  • 3,311
  • 10
  • 31
  • 62
  • If you have a `File`, use that, don't go via an `InputStream`! [The Apache POI docs explain why](http://poi.apache.org/spreadsheet/quick-guide.html#FileInputStream)! – Gagravarr Sep 08 '15 at 13:24