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

时间:2015-10-09 12:37:35

标签: java eclipse jar startup filenotfoundexception

当我尝试在我编译的.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$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)

开始申请!

以下是代码:

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文件和项目中。当我想要启动jar时,他只向我显示错误。当我在eclipse中启动项目时,他说我没有错误。

请帮帮我

1 个答案:

答案 0 :(得分:0)

线索在例外的第一行:

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调用返回一个URL,然后传递给您的方法。我猜想实际的URL是一个“jar:”URL。具体做法是:

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

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

  

获取此URL的文件名。返回的文件部分将与getPath()相同,加上getQuery()的值的串联(如果有)。如果没有查询部分,则此方法和getPath()将返回相同的结果。

您了解URL规范, URL的“路径”是第一个冒号之后的所有内容;即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);

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

相关问题