Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix "The import org.apache.poi cannot be resolved"

I want to make use of Apache-POI in my code, but am getting an error message The import org.apache.poi cannot be resolved at the import statement import org.apache.poi.xssf.usermodel.XSSFWorkbook;

I am a beginner with a freshly set up Visual Studio Code v1.30.2, Maven 3.6.0, Java JRE 1.8.0_201.

I have activated the following extensions: Debugger for Java, 0.16.0 Java dependency viewer, 0.3.0 Java extension pack, 0.5.0 Java test runner, 0.14.0 Language support for Java (TM), 0.37.0 Maven for Java, 0.14.0

I entered these statements in the pom.xml in the dependency section:

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.0.1</version>
</dependency>

I've also tried with other POI versions, e.g. 3.10-FINAL.

I'm too new to Java, Maven and VSCode to be sure I've included all the necessary information to point to a solution. Please help me :)

I've run mvn compile, with this result:

C:\Users\MYUSERNAME\Documents\Java\project2>mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------< MYNAME.project2:project2 >---------------------
[INFO] Building project2 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ project2 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\MYUSERNAME\Documents\Java\project2\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ project2 ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:\Users\MYUSERNAME\Documents\Java\project2\target\classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /C:/Users/MYUSERNAME/Documents/Java/project2/src/main/java/MYNAME/project2/App.java:[12,1] package org.apache.poi.ss.usermodel does not exist
[ERROR] /C:/Users/MYUSERNAME/Documents/Java/project2/src/main/java/MYNAME/project2/App.java:[13,37] package org.apache.poi.xssf.usermodel does not exist
[ERROR] /C:/Users/MYUSERNAME/Documents/Java/project2/src/main/java/MYNAME/project2/App.java:[14,37] package org.apache.poi.xssf.usermodel does not exist
[ERROR] /C:/Users/MYUSERNAME/Documents/Java/project2/src/main/java/MYNAME/project2/App.java:[46,9] cannot find symbol
  symbol:   class XSSFWorkbook
  location: class MYNAME.project2.App
[ERROR] /C:/Users/MYUSERNAME/Documents/Java/project2/src/main/java/MYNAME/project2/App.java:[46,45] cannot find symbol
  symbol:   class XSSFWorkbook
  location: class MYNAME.project2.App
[ERROR] /C:/Users/MYUSERNAME/Documents/Java/project2/src/main/java/MYNAME/project2/App.java:[48,9] cannot find symbol
  symbol:   class XSSFSheet
  location: class MYNAME.project2.App
[ERROR] /C:/Users/MYUSERNAME/Documents/Java/project2/src/main/java/MYNAME/project2/App.java:[55,13] cannot find symbol
  symbol:   method setCellValue(java.lang.String)
  location: variable cell of type com.google.common.collect.Table.Cell
[INFO] 7 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  5.549 s
[INFO] Finished at: 2019-01-30T09:41:45+01:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project project2: Compilation failure: Compilation failure:
[ERROR] /C:/Users/MYUSERNAME/Documents/Java/project2/src/main/java/MYNAME/project2/App.java:[12,1] package org.apache.poi.ss.usermodel does not exist
[ERROR] /C:/Users/MYUSERNAME/Documents/Java/project2/src/main/java/MYNAME/project2/App.java:[13,37] package org.apache.poi.xssf.usermodel does not exist
[ERROR] /C:/Users/MYUSERNAME/Documents/Java/project2/src/main/java/MYNAME/project2/App.java:[14,37] package org.apache.poi.xssf.usermodel does not exist
[ERROR] /C:/Users/MYUSERNAME/Documents/Java/project2/src/main/java/MYNAME/project2/App.java:[46,9] cannot find symbol
[ERROR]   symbol:   class XSSFWorkbook
[ERROR]   location: class MYNAME.project2.App
[ERROR] /C:/Users/MYUSERNAME/Documents/Java/project2/src/main/java/MYNAME/project2/App.java:[46,45] cannot find symbol
[ERROR]   symbol:   class XSSFWorkbook
[ERROR]   location: class MYNAME.project2.App
[ERROR] /C:/Users/MYUSERNAME/Documents/Java/project2/src/main/java/MYNAME/project2/App.java:[48,9] cannot find symbol
[ERROR]   symbol:   class XSSFSheet
[ERROR]   location: class MYNAME.project2.App
[ERROR] /C:/Users/MYUSERNAME/Documents/Java/project2/src/main/java/MYNAME/project2/App.java:[55,13] cannot find symbol
[ERROR]   symbol:   method setCellValue(java.lang.String)
[ERROR]   location: variable cell of type com.google.common.collect.Table.Cell
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/

I've also pasted the full output of mvn -X compile here

I've pasted my full POM.XML here

In my local folder C:\Users\MYUSERNAME.m2\repository\org\apache there is no subfolder poi.

like image 539
T C Avatar asked Oct 28 '22 20:10

T C


1 Answers

Thanks all contributors, and especially to @ayZagen (who pointed me towards the pom.xml).

It turns out that I had inserted the dependencies for poi into the dependencies section of

        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-checkstyle-plugin</artifactId>

rather than into the correct section <project> <dependencies>.

When I moved my dependencies to the correct section, maven downloads POI and that part of the code compiles.

Sorry for taking people's time over a trivial error. Thank you for helping me to find out, and asking the right questions! This was a very positive experience!

like image 120
T C Avatar answered Nov 15 '22 05:11

T C