Ich habe Schwierigkeiten, ein einfaches Programm auszuführen, das Apache POI verwendet, um ein Excel-Dokument zu erstellen. Dies ist auch mein erstes Mal mit einem Maven-Projekt, so dass vielleicht etwas damit zu tun haben:Ausführen von .jar-Datei mit Apache POI in Maven-Projekt
Mein pom.xml sieht wie folgt aus:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>calendar</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>calendar</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.10-FINAL</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.10-FINAL</version>
</dependency>
</dependencies>
</project>
Von dem, was ich sagen kann, meine Abhängigkeiten sind in Ordnung .
Das ist mein Java-Code, ich über Import-Anweisungen übersprungen, aber sie sind alle da, keine Fehler in diesem Code von dem, was ich sagen kann:
public class App
{
private static final String FILE_NAME = "/tmp/MyFirstExcel.xlsx";
public static void main(String[] args) throws IOException
{
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Datatypes in Java");
Object[][] datatypes = {
{"Datatype", "Type", "Size(in bytes)"},
{"int", "Primitive", 2},
{"float", "Primitive", 4},
{"double", "Primitive", 8},
{"char", "Primitive", 1},
{"String", "Non-Primitive", "No fixed size"}
};
int rowNum = 0;
System.out.println("Creating excel");
for(Object[] datatype : datatypes) {
Row row = sheet.createRow(rowNum++);
int colNum = 0;
for(Object field : datatype) {
Cell cell = row.createCell(colNum++);
if(field instanceof String) {
cell.setCellValue((String) field);
}
else if(field instanceof Integer) {
cell.setCellValue((Integer) field);
}
}
}
try {
FileOutputStream outputStream = new FileOutputStream(FILE_NAME);
workbook.write(outputStream);
//workbook.close()
} catch (FileNotFoundException e) {
System.out.println("Couldn't find file to write out to");
} catch (IOException e) {
System.out.println("IO Exception in printing");
}
}
}
Ich habe workbook.close()
da dies einen Fehler verursacht, kommentierte out (veraltete Methode?).
Mit dem obigen Code in meinem Quellordner kann ich mvn package
ausführen, die erfolgreich erstellt und generiert die .jar-Datei calendar-1.0-SNAPSHOT.jar
im Zielordner.
Ich bin versucht, diese Datei
java -cp target/calendar-1.0-SNAPSHOT.jar com.mycompany.app.App
... und ich bekomme die folgende Fehlermeldung
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Workbook
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.privateGetMethodRecursive(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.ss.usermodel.Workbook
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 7 more
Wenn diese Frage erfordert mehr Informationen lassen Sie mich wissen, mit laufen. Ich bin hier ratlos.
Schauen Sie hier: http://stackoverflow.com/a/574650 Sie benötigen Maven-Montage-Plugin, da im Glas nur Ihre Klassen. –
Warum verwenden Sie eine alte Version von Apache POI? Warum nicht das Neueste? – Gagravarr
Vielen Dank für Ihre Antwort! Ich folgte schließlich dem Link Alex gesendet und erreichte diesen Beitrag: http://StackOverflow.com/Questions/1814526/Problem-building-Executable-Jar-With-Maven, die mir die Antwort gab, die ich brauchte. –