英文:
JPackage (incubator) and SQLite database problem
问题
我再次需要一些帮助,已经没有想法了。:(
我正在制作一个将使用SQLite数据库的JavaFX应用程序。使用的是OpenJDK 14和OpenJFX 14。在我的Eclipse中,一切都运行得很完美。我尝试通过JPackage制作安装程序,也成功了。当我启动安装后的应用程序时,它能够正常启动,但由于某种原因拒绝连接到数据库。
这是我的项目结构:
我有一个按钮,在加载舞台时我会检查连接。它在文件" PocetnaController" 中:
//testiranje konekcije nakon instalacije
@FXML
public void btnObracunClick(ActionEvent event) {
lblStatus.setText(model.povezanaBaza());
}
以下是从被调用的"PocetnaModel"中的代码:
public String povezanaBaza() {
this.konekcija = Konekcija.poveziBazu();
return (this.konekcija != null) ? "Веза са базом успостављена" : "Повезивање са базом није успело.";
}
当为true时,它会返回我的语言中的"Веза са базом успостављена",当为false时,会返回"Повезивање са базом није успело."。
我重申,在Eclipse中一切正常。但是,当我启动安装后的应用程序时,我得到了"Повезивање са базом није успело."。因此,出于某种原因,连接返回了null。
我的数据库文件由JPackage复制到/app文件夹中。我尝试将"Porez.db"手动复制到*.exe所在的根文件夹中。奇怪的是,在这种情况下,状态标签没有更改。而它应该更改,它必须返回true或false,我不明白这一点。
当启动时,舞台的外观如下:
即使我在/app文件夹中重命名数据库文件,以便无法找到它,我的连接仍然返回false,并相应地更新状态,这是可以理解的。
以下是完整的"Konekcija"文件,它负责处理连接:
public static Connection poveziBazu() {
File baza = new File("Porez.db");
try {
if (baza.exists()) {
Connection con = DriverManager.getConnection("jdbc:sqlite:Porez.db");
return con;
} else {
return null;
}
} catch (Exception e) {
return null;
}
}
我的第一个怀疑对象是我的创建的JAR文件出了问题,但不能确定。对我来说,在Eclipse中工作但在安装后不工作没有道理。
英文:
I could use some help again, out of ideas.
I am making JavaFX application that will use SQLite database. OpenJDK 14 with OpenJFX14 is used. In my Eclipse all is working perfectly. I tried for test to make installer with JPackage and that worked too. When I start installed application it starts ok, but for some reason refuse to connect with database.
Here is my project structure:
I have a button where when Stage is loaded I check connection. It is in file PocetnaController:
//testiranje konekcije nakon instalacije
@FXML
public void btnObracunClick(ActionEvent event) {
lblStatus.setText(model.povezanaBaza());
}
And this is code from PocetnaModel that is called:
public String povezanaBaza() {
this.konekcija = Konekcija.poveziBazu();
return (this.konekcija != null) ? "Веза са базом успостављена" : "Повезивање са базом није успело.";
}
When true it returns "Database connection established" in my language and when false "Connection with database failed".
I repeat, in Eclipse all works well. But, when start installed app I am getting "Connection with database failed". So connection returning null for some reason.
My db file is coppied by JPackage in /app folder. I tried to copy manualy Porez.db to root folder where *.exe is. Strange thing, status label doesn't change in that case. And it should, it must return true or false, don't understand that.
This is stage look when started:
Even if I rename database file in /app folder, so it can't be found my connection still returning false and update status accordingly which is ok.
Here is entire Konekcija file who has a role to take care of connection:
public static Connection poveziBazu() {
File baza = new File("Porez.db");
try {
if (baza.exists()) {
Connection con = DriverManager.getConnection("jdbc:sqlite:Porez.db");
return con;
} else {
return null;
}
} catch (Exception e) {
return null;
}
}
My first suspect is something is wrong with my created JAR, but can't be sure. Makes no sense to me to work in Eclipse and no after install.
答案1
得分: 4
以下是翻译好的内容:
有几个地方你需要检查并可能修复,如果你使用以下命令运行 jpackage:
jpackage --name SQLiteTest --input C:\Users\Dalibor\Documents\NetBeansProjects\SQLiteTest\dist --main-jar SQLiteTest.jar --runtime-image hello\myjre
打开 Windows 控制台,这样你可以看到它提供的错误消息 - 使用 jpackage --win-console
。
在安装了打包的发布版本之后,检查发布目录结构。
-
确保它包含 sqllite-jdbc.jar 文件。
如果没有找到,请修复你的--input
目录以添加这个 jar 文件。 -
检查生成的应用程序的
app\xxx.cfg
文件。
它必须包含所有的 jar 依赖,包括 sqlite-jdbc.jar,例如:app.classpath=$ROOTDIR\app\jars\xxxx.jar;$ROOTDIR\app\jars\sqlite-jdbc.jar
-
检查你用来构建运行时映像
hello\myjre
的 jlink 命令,确保包含--add-modules java.sql
以与所需的 SQL 依赖项结合。 -
jpackage 构建的安装程序创建的应用程序结构与你在 Eclipse 中的结构不同 - 文件位于
app
目录下,最重要的是,只有通过管理员权限才能编辑发布版本。使用系统属性java.launcher.path
来调整 SqlLite 数据库的位置为可编辑位置,并且不要掩盖异常:public static Connection poveziBazu() { File baza = new File("Porez.db"); String jhome = System.getProperty("java.launcher.path"); if (jhome != null) { baza = new File(System.getProperty("user.home"), "Porez.db"); } System.out.println("Connecting to "+baza.getAbsolutePath()+" exists()="+baza.exists()); try { return DriverManager.getConnection("jdbc:sqlite:"+baza.getAbsolutePath()); } catch (Exception e) { System.out.println("Failed connecting to: "+baza); throw new RuntimeException("Failed to open or create db: "+baza, e); } }
英文:
There are several places you need to check and possibly fix if you ran jpackage with:
jpackage --name SQLiteTest --input C:\Users\Dalibor\Documents\NetBeansProjects\SQLiteTest\dist --main-jar SQLiteTest.jar --runtime-image hello\myjre
Turn on Windows console so that you can see the error messages it is providing - use jpackage --win-console
.
After installing your jpackaged release, check the release directory structure.
-
Make sure it contains the sqllite-jdbc.jar file.
If NOT found fix your--input
directories to add the jar. -
Check the
app\xxx.cfg
file for your generated application.
It must contain all your jar dependencies including sqlite-jdbc.jar such as:app.classpath=$ROOTDIR\app\jars\xxxx.jar;$ROOTDIR\app\jars\sqlite-jdbc.jar
-
Check that the jlink command you used to build the runtime image
hello\myjre
includes--add-modules java.sql
to combine with the required SQL dependencies. -
The app structure created by the installer that jpackage builds is different to the structure you have in Eclipse - files are under
app
directory, and most importantly the release is only editable with Administrator privileges. Use System propertyjava.launcher.path
to adjust the location of the SqlLite database to an editable location
and don't mask exceptions:public static Connection poveziBazu() { File baza = new File("Porez.db"); String jhome = System.getProperty("java.launcher.path"); if (jhome != null) { baza = new File(System.getProperty("user.home"), "Porez.db"); } System.out.println("Connecting to "+baza.getAbsolutePath()+" exists()="+baza.exists()); try { return DriverManager.getConnection("jdbc:sqlite:"+baza.getAbsolutePath()); } catch (Exception e) { System.out.println("Failed connecting to: "+baza); throw new RuntimeException("Failed to open or create db: "+baza, e); } }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论