我使用Intellij Idea构建了一个JavaFX项目。我按照这里的说明 - How to build jars from IntelliJ properly?导出了.jar文件。这是我的项目结构以及我如何配置构建路径,
我尝试通过双击来执行jar文件,但它没有执行。然后我尝试通过命令行运行它,我得到以下错误 -
imran@abdullah:~/Idea Projects/JavaFX Projects/Mukto Bangla Ovidhan Lite/out/artifacts/Mukto_Bangla_Ovidhan_Lite_jar$ sudo java -jar Mukto\ Bangla\ Ovidhan\ Lite.jar
path to 'resources/ankurdb/bn_words.db': '/home/imran/Idea Projects/JavaFX Projects/Mukto Bangla Ovidhan Lite/out/artifacts/Mukto_Bangla_Ovidhan_Lite_jar/resources' does not exist
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$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException:
file:/home/imran/Idea%20Projects/JavaFX%20%20Projects/Mukto%20Bangla%20Ovidhan%20Lite/out/artifacts/Mukto_Bangla_Ovidhan_Lite_jar/Mukto%20Bangla%20Ovidhan%20Lite.jar!/mukto/bangla/ovidhan/MainUI.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at mukto.bangla.ovidhan.Main.start(Main.java:16)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(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$49(GtkApplication.java:139)
... 1 more
Caused by: java.lang.NullPointerException
at mukto.bangla.ovidhan.DatabaseManager.GetResult(DatabaseManager.java:61)
at mukto.bangla.ovidhan.MainUIController.initialize(MainUIController.java:60)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
... 17 more
Exception running application mukto.bangla.ovidhan.Main
这是DatabaseManager.java
类 -
package mukto.bangla.ovidhan;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DatabaseManager
{
Connection connection = null;
Statement statement;
void Connect(String dbURL)
{
try{
connection = DriverManager.getConnection("jdbc:sqlite:resources"+dbURL);
statement = connection.createStatement();
statement.setQueryTimeout(30);
System.out.println("DB Connected.");
}
catch(SQLException e){
System.err.println(e.getMessage());
}
}
void InsertData(String insertQuery)
{
try{
statement.executeUpdate(insertQuery);
}
catch(SQLException e){
System.err.println(e.getMessage());
}
}
void UpdateData(String updateQuery)
{
try{
statement.executeUpdate(updateQuery);
}
catch(SQLException e){
System.err.println(e.getMessage());
}
}
void DeleteData(String deleteQuery)
{
try{
statement.executeUpdate(deleteQuery);
}
catch(SQLException e){
System.err.println(e.getMessage());
}
}
ResultSet GetResult(String resultQuery)
{
ResultSet rs=null;
try{
rs = statement.executeQuery(resultQuery);
}
catch(SQLException e){
System.err.println(e.getMessage());
}
return rs;
}
void StopConnection()
{
try{
if(connection != null)
connection.close();
}
catch(SQLException e){
System.err.println(e);
}
}
}
这是MainUIController.java
类 -
package mukto.bangla.ovidhan;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.collections.transformation.SortedList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.text.Font;
import java.net.URL;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;
import java.util.function.Predicate;
public class MainUIController implements Initializable {
DatabaseManager db=new DatabaseManager();
ObservableList<OvidhanMeaning> MeaningList;
@FXML
private TextField searchField;
@FXML
private ImageView searchIcon;
@FXML
private ImageView aboutIcon;
@FXML
private TableView<OvidhanMeaning> ovidhanTable;
@FXML
private TableColumn<OvidhanMeaning, String> englishCol;
@FXML
private TableColumn<OvidhanMeaning, String> banglaCol;
@Override
public void initialize(URL location, ResourceBundle resources) {
Image search = new Image(getClass().getResource("/images/search.png").toString(), true);
Image about = new Image(getClass().getResource("/images/about.png").toString(), true);
//Font bnFont = Font.loadFont(getClass().getResource("/fonts/Siyamrupali.ttf").toExternalForm(), 12);
Font bnFont = Font.loadFont(getClass().getResourceAsStream("/fonts/Siyamrupali.ttf"), 12);
searchIcon.setImage(search);
aboutIcon.setImage(about);
db.Connect("/ankurdb/bn_words.db");
// db.Connect("/ankurdb/Ankur_DB.db");
ResultSet rs = db.GetResult("select en_word,bn_word from words");
MeaningList=FXCollections.observableArrayList();
try {
while(rs.next())
{
String enword = rs.getString("en_word");
String bnword = rs.getString("bn_word");
MeaningList.add(new OvidhanMeaning(enword,bnword));
englishCol.setCellValueFactory(new PropertyValueFactory<OvidhanMeaning, String>("enword"));
banglaCol.setCellValueFactory(new PropertyValueFactory<OvidhanMeaning, String>("bnword"));
ovidhanTable.setItems(MeaningList);
}
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println(MeaningList.size());
FilteredList<OvidhanMeaning> filteredData = new FilteredList<>(MeaningList, p -> true);
searchField.textProperty().addListener((observable, oldValue, newValue) -> {
filteredData.setPredicate(person -> {
if (newValue == null || newValue.isEmpty()) {
return true;
}
String lowerCaseFilter = newValue.toLowerCase();
if (person.getenword().toLowerCase().contains(lowerCaseFilter)) {
return true; // Filter matches first name.
}
// else if (person.getbnword().toLowerCase().contains(lowerCaseFilter)) {
// return true; // Filter matches last name.
// }
return false; // Does not match.
});
});
SortedList<OvidhanMeaning> sortedData = new SortedList<>(filteredData);
sortedData.comparatorProperty().bind(ovidhanTable.comparatorProperty());
ovidhanTable.setItems(sortedData);
}
}
我该如何解决?如何正确导出和运行jar文件?