3

SOLVED, I GOT IT TO WORK

I just completed my first real JAVA program. It is a program which lets you open a .xlsx file and the program extracts data from this file and shows this in a textArea.

In eclipse, the program works totally fine, but the exported jar doesn't. Once the project is exported I open it in CMD with java -jar c://...... and it opens just fine. However when I try to open the excel file I get this error:

Error message

So basically one of the needed .jar files seems not to be available in runtime. However I believe all of the needed poi-3.9 and xmlbeans are available. See this:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry exported="true" kind="lib" path="src/xmlbeans-2.3.0.jar"/>
    <classpathentry exported="true" kind="lib" path="src/poi-3.9-20121203.jar"/>
    <classpathentry exported="true" kind="lib" path="src/poi-examples-3.9-20121203.jar"/>
    <classpathentry exported="true" kind="lib" path="src/poi-excelant-3.9-20121203.jar"/>
    <classpathentry exported="true" kind="lib" path="src/poi-ooxml-3.9-20121203.jar"/>
    <classpathentry exported="true" kind="lib" path="src/poi-ooxml-schemas-3.9-20121203.jar"/>
    <classpathentry exported="true" kind="lib" path="src/poi-scratchpad-3.9-20121203.jar"/>
    <classpathentry exported="true" kind="lib" path="src/log4j-1.2.13.jar"/>
    <classpathentry exported="true" kind="lib" path="src/junit-3.8.1.jar"/>
    <classpathentry exported="true" kind="lib" path="src/commons-logging-1.1.jar"/>
    <classpathentry exported="true" kind="lib" path="commons-collections4-4.1-javadoc.jar"/>
    <classpathentry exported="true" kind="lib" path="commons-collections4-4.1.jar"/>
    <classpathentry exported="true" kind="lib" path="src/commons-codec-1.5.jar"/>
    <classpathentry exported="true" kind="lib" path="poi-ooxml-3.9.jar"/>
    <classpathentry kind="src" path="src"/>
    <classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
    <classpathentry exported="true" kind="lib" path="lib/commons-codec-1.10.jar"/>
    <classpathentry exported="true" kind="lib" path="lib/commons-io-2.5.jar"/>
    <classpathentry exported="true" kind="lib" path="lib/commons-lang3-3.4.jar"/>
    <classpathentry kind="lib" path="lib/commons-logging-1.2.jar"/>
    <classpathentry exported="true" kind="lib" path="lib/cssparser-0.9.20.jar"/>
    <classpathentry exported="true" kind="lib" path="lib/htmlunit-2.23.jar"/>
    <classpathentry exported="true" kind="lib" path="lib/htmlunit-core-js-2.23.jar"/>
    <classpathentry exported="true" kind="lib" path="lib/httpclient-4.5.2.jar"/>
    <classpathentry exported="true" kind="lib" path="lib/httpcore-4.4.4.jar"/>
    <classpathentry exported="true" kind="lib" path="lib/httpmime-4.5.2.jar"/>
    <classpathentry exported="true" kind="lib" path="lib/jetty-io-9.2.18.v20160721.jar"/>
    <classpathentry exported="true" kind="lib" path="lib/jetty-util-9.2.18.v20160721.jar"/>
    <classpathentry exported="true" kind="lib" path="lib/neko-htmlunit-2.23.jar"/>
    <classpathentry exported="true" kind="lib" path="lib/sac-1.3.jar"/>
    <classpathentry exported="true" kind="lib" path="lib/serializer-2.7.2.jar"/>
    <classpathentry exported="true" kind="lib" path="lib/websocket-api-9.2.18.v20160721.jar"/>
    <classpathentry exported="true" kind="lib" path="lib/websocket-client-9.2.18.v20160721.jar"/>
    <classpathentry exported="true" kind="lib" path="lib/websocket-common-9.2.18.v20160721.jar"/>
    <classpathentry exported="true" kind="lib" path="lib/xalan-2.7.2.jar"/>
    <classpathentry exported="true" kind="lib" path="lib/xercesImpl-2.11.0.jar"/>
    <classpathentry exported="true" kind="lib" path="lib/xml-apis-1.4.01.jar"/>
    <classpathentry exported="true" kind="lib" path="src/jsoup-1.10.1.jar"/>
    <classpathentry exported="true" kind="lib" path="src/dom4j-1.6.1.jar"/>
    <classpathentry exported="true" kind="lib" path="src/selenium-server-standalone-3.0.1.jar"/>
    <classpathentry exported="true" kind="lib" path="src/stax-api-1.0.1.jar"/>
    <classpathentry kind="lib" path="src/poi-ooxml-3.11.jar"/>
    <classpathentry exported="true" kind="lib" path="src/xbean-2.0.0.jar"/>
    <classpathentry kind="output" path="bin"/>
</classpath>

See also my Order and Exporttab in the build path configurations.

Export

Am I missing something? Any help is greatly appreciated!

EDIT: I made some changes and all necessary jars are in the lib/ folder of my project. If I export jar and view the jar contents with: jar tf "location.jar". I get the following. So I believe this means that everything is exported nicely. Any suggestions on what might be going wrong, because i keep getting the same error.

Contents of JAR and Classpath

Diederik
  • 41
  • 3

1 Answers1

2

This is because eclipse reads your class-path dependencies and includes them in the runtime but for running the exported jar through the java -jar command, you will have to add the -cp <your-jar-locations-semicolon-separated> so that jvm knows that the jars are to be included in the class-path while execution.

alternatively, look into building a shaded jar which contains all your dependent jars in the exported jar.

MozenRath
  • 9,652
  • 13
  • 61
  • 104
  • thanks for your quick answer! I am not sure how to add -cp . I hava a lot of jar's, do I have to add them all in the -cp line? – Diederik Jan 12 '17 at 11:59
  • unfortunately, yes. but you can create a shaded jar also to include all the dependencies. there are ways to do it and u will find relevant questions here – MozenRath Jan 12 '17 at 12:25
  • 1
    does `-cp` work with `-jar`? From java tool description "When you use the -jar option, the specified JAR file is the source of all user classes, and other class path settings are ignored." I believe `-cp` will only be used for the main JAR file, other locations must be specified inside the manifest file. – user85421 Jan 12 '17 at 15:38
  • So based on the output that is displayed in the image from my edit, how should I specify my classpath settings inside the manifest? – Diederik Jan 12 '17 at 16:19
  • 1
    create a manifest file, e.G. "manifest.txt" (or let eclipse do it); add the [Class-Path] (http://docs.oracle.com/javase/8/docs/technotes/guides/jar/jar.html#classpath) attribute to it; and use this file when creating the JAR - assuming you are using the JAR File export tool in eclipse. – user85421 Jan 13 '17 at 09:20
  • Thanks for your answer Carlos. I did what you told me but I still get the same error. In the description of the exported Jar all necessary .jar files are exported so I am really stumped at this point why the jar has problems with finding the necessary XSSF files. Do you hava any other ideas? – Diederik Jan 13 '17 at 11:45