I'm trying to use Spire.Doc / Aspose.word to manipulate word files in a JavaFx application. When executed in eclipse or using a jar , it works very well but when I make an exe using jpackage command, the app throws java.lang.NoClassDefFoundError: com/spire/doc/Document and java.lang.ClassNotFoundException: com.spire.doc.Document
Here's the code I'm using The main class (doc.docMain)
public class docMain extends Application {
@Override
public void start(Stage primaryStage) {
FXMLLoader loader= new FXMLLoader();
loader.setLocation(docMain.class.getResource("view/Interface.fxml"));
try {
VBox interf= (VBox)loader.load();
Scene scene= new Scene(interf);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
InterfaceMap.class
public class interfaceM {
@FXML
private Button bouton;
public void createTable(){
File f= new File("C:/IOLS/createdTable.txt");
if(!f.exists()) {
System.out.println("creating the txt file");
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
LineNumberReader fr;
try {
fr = new LineNumberReader(new FileReader(f));
String str=fr.readLine();
int number=0;
System.out.println(f.length()+" Reading line: "+str);
if(str!=null && !str.trim().equals("")) {
number=Integer.valueOf(str);
number+=1;
System.out.println(number);
}
//fr.close();
FileWriter fl= new FileWriter(f);
System.out.println("Filewriter"+ number);
fl.write(number+ "\n");
fl.close();
fr.close();
System.out.println("-- creating tableWith spire --");
System.out.println("I will create a doc");
Document doc= new Document();
System.out.println("Doc created - Add a section");
Section section= doc.addSection();
String[] header= {"Nom", "Prenom", "Age", "Nationalité"};
String[][] data= {
new String[] {"Kaneza","Belyse","32 ans", "Burundaise"},
new String[] {"Kaitis", "Stephany", "24 ans", "Kenyanne"},
new String[] {"Ndolbolo", "Yasolo", "30 ans", "Congolaise"},
new String[] {"Kigeme", "Alphonsine", "65 ans", "Burundaise"},
new String[] {"Kaneza","Belyse","32 ans", "Burundaise"},
new String[] {"Kaitis", "Stephany", "24 ans", "Kenyanne"},
new String[] {"Ndolbolo", "Yasolo", "30 ans", "Congolaise"},
new String[] {"Kigeme", "Alphonsine", "65 ans", "Burundaise"}
};
System.out.println("Create the table");
Table table= section.addTable(true);
table.resetCells(data.length+1, header.length);
TableRow row=table.getRows().get(0);
row.isHeader(true);
row.setHeight(20);
row.setHeightType(TableRowHeightType.Exactly);
row.getRowFormat().setBackColor(Color.GRAY);
//add header
for(int i=0; i<header.length; i++) {
row.getCells().get(i).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
Paragraph p= row.getCells().get(i).addParagraph();
p.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
p.appendText(header[i]);
}
//add rest
for(int i=0; i<data.length; i++) {
TableRow tableR=table.getRows().get(i+1);
for(int j=0; j<data[i].length; j++) {
tableR.getCells().get(j).addParagraph().appendText(data[i][j]);
}
}
//save a file
for(int i=0; i<5; i++)
table.addRow();
table.applyHorizontalMerge(data.length+1, 0, 3);
table.applyVerticalMerge(2, data.length+2, data.length+5);
//table.getRows().get(data.length+3).getCells().get(1).splitCell(3, 3);
System.out.println("Save the file");
doc.saveToFile("C:/IOLS/createdTable-"+number+".docx", FileFormat.Docx_2013);
System.out.println("Saved");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
FXML source file
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.VBox?>
<VBox prefHeight="200.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/8"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="doc.view.interfaceM">
<children>
<Button fx:id="bouton" mnemonicParsing="false" onAction="#createTable" text="Button">
<VBox.margin>
<Insets left="174.0" right="174.0" top="90.0" />
</VBox.margin>
</Button>
</children>
The jpackage command used
jpackage --module-path path-to-javafx-jmods --add-modules javafx.controls --add-modules javafx.fxml --input directory-with-jar --name name --main-jar the-jar --main-class doc.docMain --win-shortcut --win-console.
I installed the app. When executed I get this error
3 Reading line: 11
12
Filewriter12
-- creating tableWith spire --
I will create a doc
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException:
java.lang.reflect.InvocationTargetException
at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source)
at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown
Source)
at
javafx.base@18.0.1/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown
Source)
at
javafx.base@18.0.1/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown
Source)
at
javafx.base@18.0.1/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown
Source)
at
javafx.base@18.0.1/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown
Source)
at javafx.base@18.0.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown
Source)
at javafx.base@18.0.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown
Source)
at javafx.base@18.0.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown
Source)
at javafx.base@18.0.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown
Source)
at javafx.base@18.0.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown
Source)
at javafx.base@18.0.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown
Source)
at javafx.base@18.0.1/com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at javafx.base@18.0.1/com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.base@18.0.1/javafx.event.Event.fireEvent(Unknown Source)
at javafx.graphics@18.0.1/javafx.scene.Node.fireEvent(Unknown Source)
at javafx.controls@18.0.1/javafx.scene.control.Button.fire(Unknown Source)
at
javafx.controls@18.0.1/com.sun.javafx.scene.
control.behavior.ButtonBehavior.mouseReleased(Unknown
Source)
at javafx.controls@18.0.1/com.sun.javafx.scene.control.inputmap.InputMap.handle(Unknown
Source)
at
javafx.base@18.0.1/com.sun.javafx.event.CompositeEventHandler
$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
at
javafx.base@18.0.1/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown
Source)
at
javafx.base@18.0.1/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown
Source)
at
javafx.base@18.0.1/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown
Source)
at
javafx.base@18.0.1/com.sun.javafx.event.CompositeEventDispatcher.
dispatchBubblingEvent(Unknown Source)
at javafx.base@18.0.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown
Source)
at javafx.base@18.0.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown
Source)
at javafx.base@18.0.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown
Source)
at javafx.base@18.0.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown
Source)
at javafx.base@18.0.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown
Source)
at javafx.base@18.0.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown
Source)
at javafx.base@18.0.1/com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at javafx.base@18.0.1/com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.base@18.0.1/javafx.event.Event.fireEvent(Unknown Source)
at javafx.graphics@18.0.1/javafx.scene.Scene$MouseHandler.process(Unknown Source)
at javafx.graphics@18.0.1/javafx.scene.Scene.processMouseEvent(Unknown Source)
at javafx.graphics@18.0.1/javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
at
javafx.graphics@18.0.1/com.sun.javafx.tk.quantum.
GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at
javafx.graphics@18.0.1/com.sun.javafx.tk.quantum.
GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at java.base/java.security.AccessController.doPrivileged(Unknown Source)
at javafx.graphics@18.0.1/com.sun.javafx.tk.quantum.
GlassViewEventHandler.lambda$handleMouseEvent$2(Unknown Source)
at
javafx.graphics@18.0.1/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown
Source)
at javafx.graphics@18.0.1/com.sun.javafx.tk.quantum.
GlassViewEventHandler.handleMouseEvent(Unknown Source)
at javafx.graphics@18.0.1/com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
at javafx.graphics@18.0.1/com.sun.glass.ui.View.notifyMouse(Unknown Source)
at javafx.graphics@18.0.1/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics@18.0.1/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown
Source)
at java.base/java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.reflect.Trampoline.invoke(Unknown Source)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at javafx.base@18.0.1/com.sun.javafx.reflect.MethodUtil.invoke(Unknown Source)
at javafx.fxml@18.0.1/com.sun.javafx.fxml.MethodHelper.invoke(Unknown Source)
... 47 more
Caused by: java.lang.NoClassDefFoundError: com/spire/doc/Document
at doc.view.interfaceM.createTable(interfaceM.java:60)
... 58 more
Caused by: java.lang.ClassNotFoundException: com.spire.doc.Document
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
... 59 more
I then thought that jpackage is not importing the Spire.Doc Library. I tried changing spire.doc version / javafx version / I even used aspose word but still have the same issue. Thank you for your help. I m sorry for being long.