如何启用(JAR)文件来访问应用程序中所需的外部文件夹? [英] How to enable the (JAR) file to access an external folder that is needed in the application?

查看:124
本文介绍了如何启用(JAR)文件来访问应用程序中所需的外部文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请,我的Java应用程序是假设访问包含一些其他文件的外部文件夹。它在Netbeans IDE下工作得很好,但是当我单独运行(jar)文件时却不能运行。我以为包括外部所需的文件夹在同一路径作为jar文件将解决问题,但不幸的是,它给了我相同的结果。



任何想法或建议都非常感谢!



感谢您的提前!

PS:我正在使用以下命令:java -jar MyJarFile.jar

[新增]



我的档案结构

在项目的主文件夹中,我有以下内容:

构建

build.xml <
数据:这个文件夹里面是我的日志文件(log.dat)。

dist:这个文件夹里面是我的(.jar)文件。
清单。 mf

nbproject

自述文件

src



访问文件的代码如下所示:

文件logs_File =新文件(Data+ File.separator +log.dat); p>

再次在我的NB中,这是以这种方式工作,但不是当我午餐的.JAR文件。



要知道,我一直在尝试以下方法:

文件现在访问该文件如下所示:
文件logs_F ile = new File(this.getClass()。getResource(/ Data / log.dat)。toString());

然后我复制Data文件夹以下目录:

src

build / classes /

dist


主目录



仍然没有看到文件。

预先感谢您的帮助

解决方案

那么答案如下:

我打印了 getAbsolutePath(); 我的文件,然后我把(数据)文件夹放在(dist)文件夹中(JAR)文件。我还允许(jar)文件被所有用户访问,而不仅仅是管理文件。

[已解决的主题]

感谢社区和您的帮助,关心!

亲切的问候,


Please, My Java application is suppose to access an external folder which contains some other files. It was working perfectly under the Netbeans IDE but not when i run the (jar) file alone. I thought including the external desired folder within the same path as the jar file will solve the issue but sadly it gave me the same results.

Any ideas or suggestions are greatly appreciated!

Thanks in Advance!

P.S: I am using the following command: java -jar MyJarFile.jar

[Added]

My File structure:

In the main folder of the project i have the following:

build
build.xml
Data: Inside this folder is my log file (log.dat).
dist: Inside this folder is my (.Jar) file.
manifest.mf
nbproject
README
src

The code to access the file is written as follows:

File logs_File = new File("Data" + File.separator + "log.dat");

Again within my NB it is working this way but not when i lunch the .Jar file.

[Updated]

Up to know, i have been trying the following:

The file to access the file looks as follows now: File logs_File = new File(this.getClass().getResource("/Data/log.dat").toString());

Then i copied the Data folder to following directories:
src
build/classes/
dist

and in the main directory

And still it is not seeing the file. Though this time not even netbeans is able to locate the file.

Thanks in advance for your help

解决方案

Well the answer is as follows:

I printed the getAbsolutePath(); of my file then i placed my (Data) folder inside the (dist) folder with the (JAR) file. I had also to allow the (jar) file to be accessible by all users and not just the administrative one.

[Topic Solved]

Thanks for the community and all your help and concern!

Kind Regards,

这篇关于如何启用(JAR)文件来访问应用程序中所需的外部文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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