2015-05-28 36 views
1

快速之一。我试图部署一个程序,它在以下代码中生效。我想读取一个名为,properties的属性文件。的Java - 运行JAR拒绝加载文件在同一目录

Properties props = new Properties(); 
InputStream is; 
// First try - loading from the current directory 
try { 
    File f = new File("properties"); 
    is = new FileInputStream(f); 
} catch (FileNotFoundException fnfe) { 
    fnfe.printStackTrace(System.err); 
    is = null; 
} 
try { 
    if (is == null) { 
     // Try loading from classpath 
     is = getClass().getResourceAsStream("properties"); 
    } 
    //Load properties from the file (if found), else crash and burn. 
    props.load(is); 
} catch (IOException e) { 
    e.printStackTrace(System.err); 
} 

当我通过Netbeans运行程序时,一切顺利。

当我单独运行JAR时,虽然有,但我得到两个例外。

java.io.FileNotFoundException: properties (The system cannot find the file specified) 
     at java.io.FileInputStream.open(Native Method) 
     at java.io.FileInputStream.<init>(Unknown Source) 
     . 
     . 
     .   
Exception in Application start method 
Exception in Application stop method 
java.lang.reflect.InvocationTargetException 
     . 
     . 
     . 
     (exception during props.load(is) because is == null) 

我从“dist”文件夹运行文件。我已经尝试将属性文件放在jar文件夹内,没有结果。通常,properties文件位于根项目文件夹中。

任何想法?

+0

尝试使用绝对路径到文件。当您从类路径请求文件时,请确保该文件确实位于类路径中,可以使用java VM选项的“-cp/some/dir”。还请尝试使用前导斜杠字符,以便请求的资源具有绝对路径,如“/ properties”。如果您仍然有问题,可以确认您正在使用的平台以及Java VM启动选项和命令行。 –

+0

它应该在您运行'java'命令的目录中。否则,它正在看Jar本身,这是你的第二个'尝试'。你期望它能在文件系统或jar中找到它吗? – RealSkeptic

+0

如果你想读相对于当前目录中的文件,该文件必须是在你的,不一定是罐子在目录中运行该命令的目录 –

回答

2

你读文件作为资源(getResourceAsStream("properties");)。所以它必须在类路径中。也许在jar或直接添加到类路径的目录中。

坛子是一个zip文件,这样就可以用7zip的打开它,例如添加属性文件的罐子根级别,然后重试。

+0

此。当你使用'getResourceAsStream'和相关方法时,你需要确保资源加载器能够找到它;即它必须位于classpath中。除了手动将资源添加到JAR本身之外,还有一些方法可以指定将资源文件包含在用于构建JAR的任何内容中(但我在Netbeans中不知道它们)。 –

+0

为生产目的手动添加资源是不行的。我只建议一个快速的方法来检查它是否有效。 –

+0

好吧,Netbeans似乎应该自动将资源文件打包到JAR中,只要它们位于源文件夹内的类路径中。我知道Eclipse有一些选项可以禁用这个选项,或者用构建脚本忽略非Java文件,但我不知道Netbeans中是否存在相同的麻烦。 –

0

多亏了意见,我建立了基于罐子的当前运行目录的绝对路径生成。道具给你,伙计们。

private String relativizer(String file) { 
    URL url = RobotikosAnomologitos.class.getProtectionDomain().getCodeSource().getLocation(); 
    String urlString = url.toString(); 
    int firstSlash = urlString.indexOf("/"); 
    int targetSlash = urlString.lastIndexOf("/", urlString.length() - 2) + 1; 

    return urlString.substring(firstSlash, targetSlash) + file; 
} 

所以我的新文件阅读结构为:

 Properties props = new Properties(); 
     InputStream is; 
     // First try - loading from the current directory 
     try { 
      File f = new File("properties"); 
      is = new FileInputStream(f); 
     } catch (FileNotFoundException fnfe) { 
      fnfe.printStackTrace(System.err); 
      is = null; 
     } 
     try { 
      if (is == null) { 
       // Try loading from classpath 
       String pathToProps = relativizer("properties");  
       is = new FileInputStream(new File(pathToProps)); 
       //is = getClass().getResourceAsStream(pathToProps); 
      } 
      //Load properties from the file (if found), else crash and burn. 
      props.load(is); 
     } catch (IOException e) { 
      e.printStackTrace(System.err); 
     } 
     // Finally parse the properties. 
     //code here, bla bla 
+0

这将工作,但您没有从类路径加载道具文件,只能从jar所在的目录加载。在源代码中,您可以从类路径中读取它。但是,您必须将类路径配置到文件所在的目录,或者将文件放入jar文件中。 –

相关问题