0
votes

I've made a simple dictionary using JavaFX. I've used a SQLite Database and some pictures into my application. I exported the application as a runnable JAR file using e(fx) Eclipse. I followed the steps described here - https://wiki.eclipse.org/Efxclipse/Tutorials/Tutorial1.

After exporting I opened the .JAR file. It opened successfully but wasn't working properly. It wasn't showing the results from the database and images.

When I ran the application into Eclipse workspace before building & exporting, it worked fine.

Where is the problem? How do I fix it?

Here is the code for the Controller Class function:

package imran.jfx.application;

import java.io.File;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;

public class Application_Controler implements Initializable{

    @FXML
    private HBox hBox;

    @FXML
    private Label searchLabel;

    @FXML
    private TextField searchWord;

    @FXML
    private VBox vBox;

    @FXML
    private Text BanglaMeaning;

    @FXML
    private TextArea bnMeaningTxt;

    @FXML
    private Text bAcaMeaning;

    @FXML
    private ScrollPane bAcaMeaningImg;

    @FXML
    private Label footerLabel;

    ResultSet result;
    PreparedStatement doQuery;
    Connection conn;
    String query;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        try 
        {
            Class.forName("org.sqlite.JDBC");
        } 
        catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String url="jdbc:sqlite:src/imran/ankurdb/meaning/bn_words.db";
        try 
        {
            conn = DriverManager.getConnection(url);
        } 
        catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @FXML
    void showMeaning(ActionEvent event) throws Exception {
        bnMeaningTxt.clear();

        String text=searchWord.getText();

        query = "select en_word,bn_word from words where en_word='"+text+"'";

        doQuery = conn.prepareStatement(query);
        result = doQuery.executeQuery();

        int i=0;
        while (result.next()) 
        {
            if(i==0)
            {
                bnMeaningTxt.appendText(result.getString(1) + "\t\t" + result.getString(2));
                i++;
            }
            else
                bnMeaningTxt.appendText(" , "+result.getString(2));
        }

        File file = new File("src/imran/bnacademy/meaning/"+text);
        Image image = new Image(file.toURI().toString());
        bAcaMeaningImg.setContent(new ImageView(image));
    }

}
1
Can you add the code which you are using to load the images? - ItachiUchiha
I've updated the post adding the code. Please check it now. - Abdullah Al Imran
I am guessing these images are present inside your jar and if I am guessing it right you cannot use File to load images which are present inside your jar. You need to use the classloader to load them instead. - ItachiUchiha
Then what about the database? Why the information is not showing from database? - Abdullah Al Imran
That, again, depends on how are you creating the connection to the database in the first place. I would start debugging and check if the connection is established properly or not ;) - ItachiUchiha

1 Answers

2
votes

There are multiple problems here.

  1. You are trying to create a database file inside your project. This is not going to work when you have packaged your project as a jar. You should give it a url outside your project. The database files should be placed always outside the project, so that when you package your project as a jar, they can still be created, read and written to.

  2. If the images are present inside your jar, you cannot use io.File to load images. You need to use the class loader to load them instead.

Code

URL url = getClass().getResource("/imran/bnacademy/meaning/" + text);
Image image = new Image(url.toExternalForm());