When I try to open an entry in my compiled .jar file, I get the following error (translated from german to english ;)):
java.io.FileNotFoundException: file:/tmp/guest-u9uIBd/Schreibtisch/Untitled.jar!/ressources/db_config.conf (file not found)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at java.io.FileReader.<init>(FileReader.java:58)
at application.ConnectionProperties.ReadConnectionProperties(ConnectionProperties.java:23)
at application.MainController.ReadConfigFile(MainController.java:218)
at application.MainController.initialize(MainController.java:68)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
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 application.Main.start(Main.java:15)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(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$50(GtkApplication.java:139)
at java.lang.Thread.run(Thread.java:745)
Starting application!
Here is the code:
private void ReadConfigFile() {
try { cp.ReadConnectionProperties(this.getClass().getResource("/ressources/db_config.conf"), dbhost1,
dbport1, dbusername1, dbpasswd1, dbname1);
} catch (IOException e1) {
e1.printStackTrace();
}
}
and
public void ReadConnectionProperties(URL string, TextField dbhost1, TextField dbport1, TextField dbusername1, TextField dbpasswd1,TextField dbname1) throws IOException {
String host, port, uname, password, name;
/** */
FileReader fr = new FileReader(string.getFile());
BufferedReader br = new BufferedReader(fr);
host = br.readLine();
port = br.readLine();
uname = br.readLine();
password = br.readLine();
name = br.readLine();
dbhost1.setText(host);
dbport1.setText(port);
dbusername1.setText(uname);
dbpasswd1.setText(password);
dbname1.setText(name);
br.close();
}
The Folder /ressources/ and the file db_config.conf existed in the .jar file and in the project. He show me only the error, when I want to start the jar. When I start the project in eclipse, he say me no error.
Please help me
The message is correct: you do not have a file named /tmp/guest-u9uIBd/Schreibtisch/Untitled.jar!/ressources/db_config.conf
on your computer. The URL you obtained from Class.getResource()
refers to an entry inside a jar file. It can not be read using FileReader
.
You must use the URL.openStream()
method to obtain an input stream from which you can read the contents of that entry. Or use Class.getResourceAsStream()
to save a step.
When you ran your project in eclipse, the reason it did not fail is because the classpath entry that contained the file was a directory, and so the URL returned by Class.getResource()
just happened to be a file on the local filesystem. When you package your program in a jar file, then the resource you are looking for is inside a jar rather than a local file. Using URL.openStream()
will work in both situations.
public void ReadConnectionProperties(URL url, TextField dbhost1, TextField dbport1, TextField dbusername1, TextField dbpasswd1,TextField dbname1) throws IOException {
String host, port, uname, password, name;
/** */
try ( final InputStream stream = url.openStream();
final Reader reader = new InputStreamReader(stream);
final BufferedReader br = new BufferedReader(reader); )
{
host = br.readLine();
port = br.readLine();
uname = br.readLine();
password = br.readLine();
name = br.readLine();
dbhost1.setText(host);
dbport1.setText(port);
dbusername1.setText(uname);
dbpasswd1.setText(password);
dbname1.setText(name);
}
See also try-with-resources.