JAVA: 文件:/tmp/guest-u9uIBd/Schreibtisch/Untitled.jar!/ressources/db_config.conf (找不到文件)

JAVA: file:/tmp/guest-u9uIBd/Schreibtisch/Untitled.jar!/ressources/db_config.conf (file not found)

当我尝试打开已编译的 .jar 文件中的条目时,出现以下错误(从德语翻译成英语;)):

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$launchApplication13(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait6(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null4(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater5(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(GtkApplication.java:139)
at java.lang.Thread.run(Thread.java:745)

开始申请!

代码如下:

private void ReadConfigFile() {
    try {           cp.ReadConnectionProperties(this.getClass().getResource("/ressources/db_config.conf"), dbhost1,
                dbport1, dbusername1, dbpasswd1, dbname1);
    } catch (IOException e1) {
        e1.printStackTrace();
    }
}

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();
}

文件夹 /ressources/ 和文件 db_config.conf 存在于 .jar 文件和项目中。当我想启动罐子时,他只向我显示错误。当我在eclipse中启动项目时,他说我没有错误。

请帮帮我

消息正确:您的计算机上没有名为 /tmp/guest-u9uIBd/Schreibtisch/Untitled.jar!/ressources/db_config.conf 的文件。 URL you obtained from Class.getResource() 指的是 jar 文件中的一个条目。无法使用 FileReader.

读取

您必须使用 URL.openStream() method to obtain an input stream from which you can read the contents of that entry. Or use Class.getResourceAsStream() 来保存步骤。

当你在 eclipse 中 运行 你的项目时,它没有失败的原因是因为包含该文件的类路径条目是一个目录,因此 [=12= 返回的 URL ] 恰好是本地文件系统上的一个文件。当您将程序打包在 jar 文件中时,您要查找的资源就在 jar 中而不是本地文件中。使用 URL.openStream() 在这两种情况下都有效。

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);
        }

另见 try-with-resources

线索在异常的第一行:

java.io.FileNotFoundException: 
  file:/tmp/guest-u9uIBd/Schreibtisch/Untitled.jar!/ressources/db_config.conf 
  (file not found)

该名称不是文件名。这是一个URL。您不能使用 FileReader(String) 打开资源。首先,您尝试读取的资源不是文件。它实际上是一个文件的组成部分。

那么让我们看一下代码的相关部分:

cp.ReadConnectionProperties(
    this.getClass().getResource("/ressources/db_config.conf"), ...);

public void ReadConnectionProperties(URL string, ...) throws IOException {
    FileReader fr = new FileReader(string.getFile());

getResource 调用 returns 一个 URL,然后将其传递给您的方法。我会猜测实际的 URL 是 "jar:" URL。具体来说:

jar:file:/tmp/guest-u9uIBd/Schreibtisch/Untitled.jar!/ressources/db_config.conf

该方法调用 URL.getFile() ... 但这里是 javadoc 所说的 getFile():

Gets the file name of this URL. The returned file portion will be the same as getPath(), plus the concatenation of the value of getQuery(), if any. If there is no query portion, this method and getPath() will return identical results.

如果你理解URL规范,thisURL的"path"是第一个冒号之后的所有内容;即 file:/tmp/guest-u9uIBd/Schreibtisch/Untitled.jar!/ressources/db_config.conf

然后您尝试使用那个 "file:" URL 就好像它是一个文件路径名一样。


那么解决方法是什么?嗯,有很多,但一个简单的是:

cp.ReadConnectionProperties(
    this.getClass().getResourceAsStream("/ressources/db_config.conf"), ...);

public void ReadConnectionProperties(InputStream is, ...) throws IOException {
    Reader r = new InputStreamReader(is);

(请注意,reader 将使用平台默认字符编码方案读取资源。这可能不是正确的做法。)