java-Jar文件可以';不能在另一台电脑上执行
我有一个从我的程序编译的可执行jar文件,我在PC上运行它。当我在命令提示符下使用java-Jar文件可以';不能在另一台电脑上执行,java,Java,我有一个从我的程序编译的可执行jar文件,我在PC上运行它。当我在命令提示符下使用java-jar[nameofjar.jar] 但是,我尝试在另一台pc上测试它。使用命令提示符运行同一个jar文件时,它会抛出一个错误: D:\QA06122018_2>java -jar Indexing.jar java.lang.NullPointerException at IndexDriver.processText(IndexDriver.java:81) at
java-jar[nameofjar.jar]
但是,我尝试在另一台pc上测试它。使用命令提示符运行同一个jar文件时,它会抛出一个错误:
D:\QA06122018_2>java -jar Indexing.jar
java.lang.NullPointerException
at IndexDriver.processText(IndexDriver.java:81)
at IndexDriver.index(IndexDriver.java:140)
at Main.main(Main.java:44).....
两台电脑使用相同的操作系统和设置。
我甚至查看了有关错误的代码,似乎没有任何问题。在我的IDE上运行良好
有什么我可能忽略的吗
编辑:
守则:
public PreparedStatement preparedStatement = null;
MysqlAccessIndex con = new MysqlAccessIndex();
public Connection con1 = con.connect();
String path1;
public void index() throws Exception {
// Connection con1 = con.connect();
try {
Statement statement = con1.createStatement();
ResultSet rs = statement.executeQuery("select * from filequeue where Status='Active' LIMIT 5");
while (rs.next()) {
// get the filepath of the PDF document
path1 = rs.getString(2);
int getNum = rs.getInt(1);
Statement test = con1.createStatement();
test.executeUpdate("update filequeue SET STATUS ='Processing' where UniqueID="+getNum);
try {
// call the index function
PDDocument document = PDDocument.load(new File(path1),MemoryUsageSetting.setupTempFileOnly());
if (!document.isEncrypted()) {
PDFTextStripper tStripper = new PDFTextStripper();
for(int p=1; p<=document.getNumberOfPages();++p) {
tStripper.setStartPage(p);
tStripper.setEndPage(p);
try {
String pdfFileInText = tStripper.getText(document);
processText(pdfFileInText);
System.out.println("Page "+p+" done");
}catch (Exception e){
e.printStackTrace();
Statement statement1 = con1.createStatement();
statement1.executeUpdate("update filequeue SET Error ='E0003' where UniqueID="+getNum);
statement1.executeUpdate("update filequeue SET Status ='Error' where UniqueID="+getNum);
con1.commit();
con1.close();
}
}
}
// After completing the process, update status: Complete
Statement pre= con1.createStatement();
pre.executeUpdate("update filequeue SET STATUS ='Complete' where UniqueID="+getNum);
// con1.commit();
preparedStatement.close();
document.close();
System.out.println("Successfully commited changes to the database!");
con1.commit();
// con1.close();
// updateComplete_DB(getNum);
} catch (Exception e) {
try {
System.err.println(e);
Statement statement1 = con1.createStatement();
statement1.executeUpdate("update filequeue SET STATUS ='Error' where UniqueID="+getNum);
statement1.executeUpdate("update filequeue SET Error ='E0002' where UniqueID="+getNum);
con1.commit();
// add rollback function
rollbackEntries();
}catch (Exception e1){
System.out.println("Could not rollback updates :" + e1.getMessage());
}
}
// con1.close();
}
}catch(Exception e){
e.printStackTrace();
//System.out.println("lalala");
}
//con1.commit();
con1.close();
}
根本原因是没有要处理的行 您似乎只在
for(String-line:lines){
循环内创建了准备好的语句,但您只关闭了创建的最后一条语句(在该循环外)
当您没有任何行时,preparedStatement
为空,因为您从未创建过一行
即使有行要处理,也要创建大量准备好的语句,但只关闭最后一行
您可能应该在方法开始时创建一个准备好的语句,并在整个方法中重复使用它,最后关闭它。如果我们看不到抛出NPE的代码,我们不知道您可能忽略了什么。请您的问题提供一个。@Slaw我编辑了我的帖子。但是您如何解释它在我的PC上运行良好d不在另一台上?@Daredevil您仍然没有发布完整的代码(例如,
processText
中的con1
是什么?),其他人不可能“解释它在我的电脑上运行良好,而不是在另一台电脑上”。或者答案应该是“因为您犯了一些错误”.NullPointerException堆栈跟踪告诉您从何处开始查找:IndexDriver.java第81行。遗憾的是,您没有指出第81行的位置。第81行-指向preparedStatement.close()
inside processText Method在开始时创建一条准备好的语句并重用它是什么意思?一条准备好的语句可以在多次执行中重用。每次执行时都会用希望使用的值替换“?”占位符。这是一个主要好处(除了消除SQL注入之外)准备一条语句并多次重复使用它要比执行大量常规语句快得多。当您试图解决所有这些问题时,我建议添加调试日志记录,以便您可以在执行过程中查看变量的值以及正在执行的代码路径。这将帮助您了解错误在代码中的位置。您已经在为每个单词重用语句,只需将语句的创建移到“line”循环之外。因此,根据我从您的回答中了解的情况,我必须将初始prepareStatement(连同sql声明)移到循环之外,然后是preparedstatement.close()能保持原样吗?
public void processText(String text) throws SQLException {
String lines[] = text.split("\\r?\\n");
for (String line : lines) {
String[] words = line.split(" ");
String sql="insert IGNORE into test.indextable values (?,?);";
preparedStatement = con1.prepareStatement(sql);
int i=0;
for (String word : words) {
// check if one or more special characters at end of string then remove OR
// check special characters in beginning of the string then remove
// insert every word directly to table db
word=word.replaceAll("([\\W]+$)|(^[\\W]+)", "");
preparedStatement.setString(1, path1);
preparedStatement.setString(2, word);
preparedStatement.executeUpdate();
}
}
preparedStatement.close();
}