0

recently i found this stackoverflow page enter link description here "JavaFx:Export TableView to excel with name of columns", i run this code and i got the following issue

import org.apache.poi.ss.usermodel.Row;
                                  ^
TableViewExample.java:13: error: package org.apache.poi.ss.usermodel does not exist
import org.apache.poi.ss.usermodel.Sheet;
                                  ^
TableViewExample.java:14: error: package org.apache.poi.ss.usermodel does not exist
import org.apache.poi.ss.usermodel.Workbook;
                                  ^
TableViewExample.java:42: error: cannot find symbol
        Workbook workbook = new HSSFWorkbook();
        ^
  symbol:   class Workbook
  location: class TableViewExample
TableViewExample.java:43: error: cannot find symbol
        Sheet spreadsheet = workbook.createSheet("sample");
        ^
  symbol:   class Sheet
  location: class TableViewExample
TableViewExample.java:45: error: cannot find symbol
        Row row = spreadsheet.createRow(0);
        ^
  symbol:   class Row
  location: class TableViewExample
6 errors 

According to what is described there, a library named Apache poi is being used. i used this library in order to run this code poi-3.0.2.jar in this way

javac -classpath ".:poi-3.0.2.jar" TableViewExample.java

using command line in linux ubuntu, i compiled archives.java before with great success using command line. Other persons have used jakarta-poi-3.0.2.jar but this .jar file library does not exist anymore and i used poi-3.0.2.jar instead.

Now i used the new poi-5.1.0.jar and i got the following error message

library Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/io/output/UnsynchronizedByteArrayOutputStream
    at TableViewExample.start(TableViewExample.java:42)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication.lambda$null$10(GtkApplication.java:245)
    ... 1 more
Caused by: java.lang.ClassNotFoundException: org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
    ... 10 more

Related to "java.lang.ClassNotFoundException: org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream"

any information about how to solve this issue and make this piece of code work?

Thank you in advance

1 Answers1

3

The most recent apache poi library is 5.1.0, I do not recommend using old versions. The download is here, though I don't recommend directly downloading libraries either, instead add them as dependencies using a build tool like maven or gradle.

When you look at the project file for poi, it has lots of dependencies, so simply adding the poi jar to the classpath is not enough, instead, let your build tool work it out.

Modern JavaFX distributions (which you should be using) are modular, so you need to either have a module-info file or command-line arguments for the module path and modules added (which you don't have in your example).

If you have a module-info, then you may need to require modules for poi to work (I don't know the command for that).

It may be easier to run without a module-info as long as you have correct module specifications for JavaFX on your command line (as defined by the openjfx.io getting started doc for non-modular applications).

FAQ

i am thinking now in another solutions in how to convert a JavaFX tableview to excel

I agree, don’t use poi, instead write the data to a csv file. You can search for solutions on how to write data in csv format using Java. That is what I would do.

now i implemented the new poi library poi-5.1.0.jar and i got the error message

java.lang.ClassNotFoundException: org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream

During execution, you are still missing the required transitive dependencies required to run the application.

If you use poi, one way to get the required dependencies is to ask maven to copy them to a lib directory:

then when you execute your app jar, have the lib directory for your app on your classpath and the JavaFX SDK lib directory on your module path.

i can create .csv files but unfortunately client ask .xls files

POI can create the xls files directly, or you could run an external tool to do the csv to xls conversion:

The tool could be included in your application packaging and installation and invoked from java:

jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • Hello, thank you very much for your help, now i implemented the new poi library poi-5.1.0.jar and i got the error message described above related to "java.lang.ClassNotFoundException: org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream" any information about this issue? i am thinking now in another solutions in how to convert a JavaFX tableview to excell, thank you very much in advance – Omar Elio Torres Castillo Dec 09 '21 at 04:26
  • 1
    Thank you very much for your help and support, actually i can create .csv archives readable by excell as you suggested, i found that there is a way that java can convert now .csv files to excell files, thank you very much – Omar Elio Torres Castillo Dec 09 '21 at 23:57
  • @OmarElioTorresCastillo if you wish you can add a [self-answer](https://stackoverflow.com/help/self-answer) with your csv solution. – jewelsea Dec 10 '21 at 06:56
  • Excuse me, what is a self-answer??? – Omar Elio Torres Castillo Dec 11 '21 at 06:58
  • Thank you very much for your support, i can create .csv files but unfortunately client ask .xls files, so i am still looking for a solution of this, i will continue making a research and if i find the answer i will make a self-answer here, thank you – Omar Elio Torres Castillo Dec 11 '21 at 07:01