Java中项目文件夹的绝对路径 [英] Absolute Path of Project's folder in Java

查看:33
本文介绍了Java中项目文件夹的绝对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个话题有很多混乱.已经问了几个问题.事情似乎还不清楚.类加载器、绝对文件路径等

假设我有一个项目目录结构,

<前>我的项目 ---dist--lib--src- 测试

我在lib/txt"目录中有一个资源说txtfile.txt".我想以独立于系统的方式访问它.我需要项目的绝对路径.所以我可以将路径编码为 abspath+"/lib/Dictionary/txtfile.txt"

假设我这样做

 java.io.File file = new java.io.File("");//虚拟文件String abspath=file.getAbsolutePath();

我得到当前工作目录,它不一定是项目根目录.

假设我从dist"文件夹中执行最终的prj.jar",该文件夹还包含lib/txt/txtfile.txt"目录结构和资源,它也应该在这里工作.我应该是dist文件夹的绝对路径.

希望问题已经解决.

解决方案

你真的应该使用 getResource()getResourceAsStream() 使用您的类加载器进行此类操作.特别是,这些方法使用您的类加载器来确定项目中资源的搜索上下文.

指定诸如 getClass().getResource("lib/txtfile.txt") 之类的内容以获取文本文件.

澄清:与其考虑如何获取资源的路径,您应该考虑获取资源——在这种情况下,某个目录中的文件(可能在您的 JAR 中).在这种情况下不需要知道一些绝对路径,只需要一些 URL 来获取文件,ClassLoader 会为你返回这个 URL.如果您想打开文件流,您可以直接执行此操作,而无需使用 getResourceAsStream 处理 URL.

您尝试通过类加载器访问的资源需要位于类路径上(在 JAR 文件的清单中配置).这很关键!ClassLoader 使用 Class-Path 来查找资源,因此如果您没有在 Class-Path 中提供足够的上下文,它将无法找到任何内容.如果添加 .,ClassLoader 应该根据您引用资源的方式解析 JAR 内部或外部的任何内容,但您当然可以更具体.

引用带有 前缀的资源. 将导致 ClassLoader 也寻找 JAR 之外的文件,而不用句点前缀资源路径将指示 ClassLoader 只在 JAR 外部查找文件JAR 文件.

这意味着如果你有一些文件inside JAR 目录lib 名为foo.txt 并且你想要获取资源然后你会运行 getResource("lib/foo.txt");

如果相同的资源在 JAR 之外,您将运行 getResource("./lib/foo.txt");

Lots of confusion in this topic. Several Questions have been asked. Things still seem unclear. ClassLoader, Absolute File Paths etc etc

Suppose I have a project directory structure as,

MyProject--
            --dist        
            --lib
            --src
            --test

I have a resource say "txtfile.txt" in "lib/txt" directory. I want to access it in a system independent way. I need the absolute path of the project. So I can code the path as abspath+"/lib/Dictionary/txtfile.txt"

Suppose I do this

 java.io.File file = new java.io.File("");   //Dummy file
    String  abspath=file.getAbsolutePath();

I get the current working directory which is not necessarily project root.

Suppose I execute the final 'prj.jar' from the 'dist' folder which also contains "lib/txt/txtfile.txt" directory structure and resource,It should work here too. I should absolute path of dist folder.

Hope the problem is clear.

解决方案

You should really be using getResource() or getResourceAsStream() using your class loader for this sort of thing. In particular, these methods use your ClassLoader to determine the search context for resources within your project.

Specify something like getClass().getResource("lib/txtfile.txt") in order to pick up the text file.

To clarify: instead of thinking about how to get the path of the resource you ought to be thinking about getting the resource -- in this case a file in a directory somewhere (possibly inside your JAR). It's not necessary to know some absolute path in this case, only some URL to get at the file, and the ClassLoader will return this URL for you. If you want to open a stream to the file you can do this directly without messing around with a URL using getResourceAsStream.

The resources you're trying to access through the ClassLoader need to be on the Class-Path (configured in the Manifest of your JAR file). This is critical! The ClassLoader uses the Class-Path to find the resources, so if you don't provide enough context in the Class-Path it won't be able to find anything. If you add . the ClassLoader should resolve anything inside or outside of the JAR depending on how you refer to the resource, though you can certainly be more specific.

Referring to the resource prefixed with a . will cause the ClassLoader to also look for files outside of the JAR, while not prefixing the resource path with a period will direct the ClassLoader to look only inside the JAR file.

That means if you have some file inside the JAR in a directory lib with name foo.txt and you want to get the resource then you'd run getResource("lib/foo.txt");

If the same resource were outside the JAR you'd run getResource("./lib/foo.txt");

这篇关于Java中项目文件夹的绝对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆