Opening .xlsx file with Apache POI gives NoClassDefFoundError InvalidFormatException

前端 未结 1 1789
遥遥无期
遥遥无期 2021-01-22 23:37

I\'m having trouble getting the import of all the required .jar files working correctly. My end goal is simply to read in an excel spreadsheet (more specifically .xlsx files) to

相关标签:
1条回答
  • 2021-01-22 23:46

    The problem is with how you're launching your program. I see you currently have a shell script of:

    javac -classpath "/home/robsco/Webdev/java/poi/*" TestExcel.java
    java TestExcel
    

    That compiles with all the jars, but doesn't run with them, so your program won't have its dependencies available. (Unlike with some c programs, compiling with Java won't inline the depdendencies, nor will it add local links to the files. There are tools that will do that for you if you really need it though)

    If you change your shell script to be:

    javac -classpath "/home/robsco/Webdev/java/poi/*" TestExcel.java
    java -classpath ".:/home/robsco/Webdev/java/poi/*" TestExcel
    

    It should work fine. Note the addition of the current directory in the classpath in order for 'java' to find your class (in this case TestExcel).

    Alternately, you could do something a little icky with environment variables, using the CLASSPATH envvar that Java checks, eg

    export CLASSPATH=.:`ls -1d --color=no /home/robsco/Webdev/java/poi/* | xargs echo | sed 's/ /\:/g'`
    javac TestExcel.java
    java TestExcel
    
    0 讨论(0)
提交回复
热议问题