我遇到过这样的问题。这是我的代码:
package pdfex;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.control.TextField;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.stage.FileChooser;
import javafx.geometry.*;
import javafx.scene.paint.Color;
import java.io.File;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.lang.*;
import java.util.*;
import java.util.HashMap;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.ColumnText;
//import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Element;
public class PdfExport extends Application {
public static void main(String[] args) {
launch(args);
}
File filePDF;
File fileCos;
File fileWmark;
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("PDF Export");
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25, 25, 25, 25));
Text scenetitle = new Text("PDF Export");
scenetitle.setFont(Font.font("Tahoma",FontWeight.NORMAL,20));
grid.add(scenetitle, 0, 0, 2, 1);
Label openPwd = new Label("Open Password");
grid.add(openPwd, 0, 1);
TextField userTextField = new TextField();
userTextField.getText();
grid.add(userTextField, 1, 1);
Label homePwd = new Label("Home Password");
grid.add(homePwd, 0, 2);
TextField pwBox = new TextField();
pwBox.getText();
grid.add(pwBox, 1, 2);
Label labelCos = new Label();
//final String cos = new String();
Button btnBrowse = new Button("List of Companies");
final StringBuilder cos = new StringBuilder();
btnBrowse.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(final ActionEvent e) {
FileChooser listChooser = new FileChooser();
//Set filter TEXT file
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("TEXT Files (*.txt)", "*.txt");
listChooser.getExtensionFilters().add(extFilter);
fileCos = listChooser.showOpenDialog(primaryStage);
String filePathCos = fileCos.getAbsolutePath();
labelCos.setText(filePathCos);
cos.append(filePathCos);
}
});
HBox hbBtnBrowser = new HBox(10);
hbBtnBrowser.setAlignment(Pos.CENTER);
hbBtnBrowser.getChildren().addAll(btnBrowse, labelCos);
grid.add(hbBtnBrowser, 1, 3);
Label labelPDF = new Label();
//final String pdf = new String();
Button btnBrowse2 = new Button("Look for PDF files");
final StringBuilder pdf = new StringBuilder();
btnBrowse2.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
FileChooser pdfFile = new FileChooser();
// Set extension filtre
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("PDF Files (*.pdf)", "*.pdf");
pdfFile.getExtensionFilters().add(extFilter);
// Show open file dialog
filePDF = pdfFile.showOpenDialog(primaryStage);
String filePathPDF = filePDF.getAbsolutePath();
labelPDF.setText(filePathPDF);
pdf.append(filePathPDF);
}
});
HBox hbBtnBrowse2 = new HBox(10);
hbBtnBrowse2.setAlignment(Pos.CENTER);
hbBtnBrowse2.getChildren().addAll(btnBrowse2, labelPDF);
grid.add(hbBtnBrowse2, 1, 4);
// Watermark File
Label labelWatermark = new Label();
//final String wmark = new String();
Button btnWmark = new Button("Watermark?");
final StringBuilder wmark = new StringBuilder();
btnWmark.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
FileChooser wmarkFile = new FileChooser();
fileWmark = wmarkFile.showOpenDialog(primaryStage);
String filePathWmark = fileWmark.getAbsolutePath();
labelWatermark.setText(filePathWmark);
wmark.append(filePathWmark);
}
});
HBox hbBtnBrowse3 = new HBox(10);
hbBtnBrowse3.setAlignment(Pos.CENTER);
hbBtnBrowse3.getChildren().addAll(btnWmark, labelWatermark);
grid.add(hbBtnBrowse3, 1, 5);
//grid.setGridLinesVisible(true);
Button btn = new Button("Exporting PDF");
HBox hbBtn = new HBox(10);
hbBtn.setAlignment(Pos.BOTTOM_RIGHT);
hbBtn.getChildren().add(btn);
grid.add(hbBtn, 2, 7);
final Text actiontarget = new Text();
grid.add(actiontarget, 1, 9);
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
long startTime = System.currentTimeMillis();
//System.out.println(cos.toString());
actiontarget.setFill(Color.BLACK);
try {
BufferedReader br = new BufferedReader(new FileReader(cos.toString()));
String line;
String user = userTextField.getText();
byte[] USER = user.getBytes();
String owner = pwBox.getText();
byte[] OWNER = owner.getBytes();
if(user != owner) {
try{
while ((line = br.readLine()) != null) {
try {
String a = line;
PdfReader Post_Survey = new PdfReader(pdf.toString());
int number_of_pages = Post_Survey.getNumberOfPages();
// Naming PDF files
PdfStamper stamp = new PdfStamper(Post_Survey, new FileOutputStream(line + ".pdf"));
// set Security
stamp.setEncryption(USER, OWNER, PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128 | PdfWriter.DO_NOT_ENCRYPT_METADATA);
// Update Metadata
HashMap<String, String> hMap = Post_Survey.getInfo();
hMap.put("Title", "Post Survey Highlights");
hMap.put("Subject", "Market Highlights");
hMap.put("Creator", "Zestiremia");
hMap.put("Keywords"," \n * * * * * \n * * \n * * * * * * \n * * * \n * * * \n * * * * * * \n * * \n * * \n * * * * * * * * \n ");
hMap.put("Author", "SSC-Talentnet");
stamp.setMoreInfo(hMap);
int i = 0;
Image watermark_image = Image.getInstance(wmark.toString());
BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ITALIC, BaseFont.WINANSI, BaseFont.EMBEDDED);
watermark_image.setAbsolutePosition(430, 10);
PdfContentByte add_watermark;
PdfContentByte add_text;
//start stamping!!
while (i < number_of_pages) {
i++;
// watermark image
add_watermark = stamp.getOverContent(i);
add_watermark.addImage(watermark_image);
// set where lines are
add_text = stamp.getOverContent(i);
add_text.setFontAndSize(bf, 12);
ColumnText.showTextAligned(add_text,Element.ALIGN_LEFT, new Phrase("This is the properties of blah blah and is solely used for " + line + " only!"), 20, 20, 0);
}stamp.close();
//Post_Survey.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}catch (Exception e) {
e.printStackTrace();
}
}
}catch (IOException e) {
e.printStackTrace();}
long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;
actiontarget.setText("What up yo! It's all done.\nThe whole process has taken up to "+totalTime/1000+"s.\nYou've done a great job!");
//System.out.println("What up yo!!!");
}
});
primaryStage.setScene(new Scene(grid, 550, 300));
primaryStage.show();
}
}
如果我编译
PS D:\Documents\javafx> javac PdfExport.java
在pdfex文件夹中,然后
cd ..
跑,
`java pdfex.PdfExport程序没问题。
但是,如果我运行以下内容:
PS D:\Documents\javafx> javac -d classes PdfExport.java
PS D:\Documents\javafx> javapackager -createjar -appclass pdfex.PdfExport -srcdir classes -outdir dist -outfile pdfex2.jar -v
编辑很好,但是当我运行程序时,
PS D:\Documents\javafx\dist> java -jar pdfex2.jar
这是我得到的:
Exception in thread "JavaFX Application Thread" java.lang.NoClassDefFoundError: com/itextpdf/text/pdf/PdfReader
at pdfex.PdfExport$4.handle(PdfExport.java:232)
at pdfex.PdfExport$4.handle(PdfExport.java:199)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Node.fireEvent(Unknown Source)
at javafx.scene.control.Button.fire(Unknown Source)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Scene$MouseHandler.process(Unknown Source)
at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source)
at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.notifyMouse(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$141(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/96639997.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: com.itextpdf.text.pdf.PdfReader
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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)
... 48 more
` 知道为什么吗?
答案 0 :(得分:0)
如果您使用java -jar,则需要在jar的MANIFEST中引用其他java库
答案 1 :(得分:0)
就像tomsontom所说,Java运行时正在寻找无法使用的类/库。
一种流行的方法是将项目设置为使用Maven。 Maven可以从maven central中提取库。然后你可以使用maven shade plugin创建.jar,并在其中打包库。
另一个选择是使用另一个构建工具Gradle。