简体   繁体   English

Java - 类路径问题

[英]Java - Classpath issue

I have a products.jar file. 我有一个products.jar文件。 Inside that there is one class com.ClassA . 里面有一个类com.ClassA The structure of the project is like 项目的结构就像

products
    |--test
        |--com
            |--ClassA.java
        |--dependencies
            |---inputs.txt

Inside eclipse, ClassA.java is accessing the inputs.txt file by the following path and it is working fine 在Eclipse中, ClassA.java正在访问inputs.txt通过以下路径的文件,它工作正常

private static final String PROPERTIES_FILE_PATH = "test/dependencies/inputs.txt";

test package is in the java build path -> sources test包是在java构建路径 - > sources中

But when I am exporting this project as products.jar , I find that in the jar file there is no test directory. 但是当我将这个项目导出为products.jar ,我发现在jar文件中没有test目录。 There are two dirs com and dependencies at the root of the jar file. jar文件的根目录下有两个dirs comdependencies So when I am trying to execute the ClassA (residing in jar file) through command line, I am getting the following exception: 因此,当我尝试通过命令行执行ClassA (驻留在jar文件中)时,我收到以下异常:

JUnit version 4.8.2
java.io.FileNotFoundException: test/dependencies/inputs.txt (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:137)
at java.io.FileInputStream.<init>(FileInputStream.java:96)

So after finding that test dir is not being exported in jar file, I changed the path of the file in my ClassA.java to dependencies/inputs.txt . 因此,在发现未在jar文件中导出test目录之后,我将ClassA.java中文件的路径更改为dependencies/inputs.txt inputs.txt。 It didn't work in eclipse but I thought that it would work in jar because jar file is in classpath and dependencies folder is at the root of the jar file so the java launcher will be able to locate the dependencies folder and then inputs.txt file. 它在eclipse中不起作用,但我认为它可以在jar中工作,因为jar文件在classpath中, dependencies文件夹位于jar文件的根目录下,因此java启动程序将能够找到dependencies文件夹,然后输入inputs.txt文件。

But unfortunately it is also not working. 但不幸的是它也没有用。

You can't use FileInputStream to read a file packed inside a JAR - it's not a file any more. 您不能使用FileInputStream来读取JAR中打包的文件 - 它不再是文件。 FileInputStream only works for actual disk files. FileInputStream仅适用于实际的磁盘文件。

You need to read the resource using Class.getResourceAsStream ( javadoc ), eg 您需要使用Class.getResourceAsStreamjavadoc )读取资源,例如

InputStream stream = getClass().getResourceAsStream("/dependencies/inputs.txt");

You leave off the test prefix because it's not in the JAR file structure. 您不使用test前缀,因为它不在JAR文件结构中。

FileInputStream() will not read inside your jar, only files on the file system. FileInputStream()不会在jar中读取,只读取文件系统上的文件。 Use .getClass().getResourceAsStream(resourcename) or .getClass().getClassLoader().getResourceAsStream(resourcename) 使用.getClass()。getResourceAsStream(resourcename).getClass()。getClassLoader()。getResourceAsStream(resourcename)

More info on the javadocs for class getResourceAsStream and classloader getResourceAsStream() 关于类getResourceAsStreamclassloader的javadocs的更多信息getResourceAsStream()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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