Opening .xlsx file with Apache POI gives NoClassDefFoundError InvalidFormatException

旧巷老猫 提交于 2019-12-02 01:45:36
Gagravarr

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!