简体   繁体   中英

Exporting IntelliJ project to JAR. Path to files stop working

I have a project I'm trying to export to a JAR file. Building the JAR itself works fine and it seems like all the necessary files are being included into the JAR. The problem I have is when I have code like this in one of my classes:

Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:player/resources/test.db");

It is also not working for this code:

    FXMLLoader load = new FXMLLoader(getClass().getResource("commonGUI.fxml"));

Running the project through IntelliJ works fine but when building and running the JAR file, I get this exception:

java.sql.SQLException: path to 'player/resources/test.db': '/Users/Kristian/Documents/Uni/Freelance/SEProjects/SE18/working/Plookify/out/artifacts/Plookify_jar/player' does not exist

or this depending on which file I'm trying to load:

Caused by: javafx.fxml.LoadException: 
file:/Users/Kristian/Documents/Uni/Freelance/SEProjects/SE18/working/Plookify/out/artifacts/Plookify_jar/Plookify.jar!/common/commonGUI.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 common.openMainGUI.start(openMainGUI.java:21)
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)
Caused by: java.lang.IllegalStateException: Location is not set.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at common.commonGUIController.loadPlayerPane(commonGUIController.java:43)
at common.commonGUIController.initialize(commonGUIController.java:31)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
... 14 more

How to solve an issue like this?


Solved

Problem has been solved. To solve the SQLException, the DB file had to be placed outside the JAR file. For the FXML error, the paths in the project code had to be modified.

You don't say what was done to setup your project in IntelliJ so I assume you added the Jar at some point.

You need to add the SQLite JDBC driver to your classpath. It is found when running inside of your IDE but not from the command line.

You can get more information here: Java: how to import a jar file from command line and http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM