我应该在哪里放置我的文件,以便在我运行jar时能够访问它们? [英] Where should i place my files in order to be able to access them when i run the jar?

查看:94
本文介绍了我应该在哪里放置我的文件,以便在我运行jar时能够访问它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一些对象存储在文件中,一切都在Netbeans控制台中正常工作。但是当我通过命令行(java -jar myapp.jar)运行JAR时,我得到了这个:





我应该在哪里放置文件,以便能够通过命令行运行应用程序?

解决方案

答案取决于您是否要写入文件......



你可以......



确保文件相对于Jar文件放置并使用相对路径。这个问题是如果执行上下文与jar和文件存储的目录不同,你将无法再找到它们......



这也将取决于您的构建过程,以确保将任何所需的文件复制到Jar的构建位置



您可以......



将文件放在众所周知的位置,例如,在Windows上,您可以使用 {user.home} / AppData / Local / {application name} 或在Mac上你可以使用 {user.home} / Library / Application Support / {application name} 并将文件放在这里,然后就可以使用了文件的绝对路径



这可能会成为安装问题,因为您需要确保在安装应用程序时将所有必需的文件复制到所需的位置。 / p>

如果文件是在运行时自动生成的,那么你只需要确保目录存在并在它们不存在时制作它们



你可以......



将文件存储在Jar上下文(AKA嵌入资源)中,您执行此操作取决于您的IDE和构建过程,例如,在Netbeans中,您可以将文件复制到项目的 src 目录中,它们将自动包含在内在生成的Jar文件中。我相信Eclipse可以以相同的方式工作。但是,如果您使用的是Maven,则需要将文件放入 resources 目录中。



然后,您将使用 Class#getResource Class#getResourceAsStream ,具体取决于您的需求。



但这会使资源成为只读。


I store some objects in files and everything works fine in Netbeans console. But when i run the JAR through the command line (java -jar myapp.jar) i get this:

Where should i place the files in order to be able run the app through the command line?

解决方案

The answer will depend on if you want to write to the files or not...

You could...

Ensure that the files are placed relative to the Jar file and use a relative path. The problem with this is if the execution context is not the same directory as where the jar and files are stored, you won't be able to find them again...

This will also be dependent on your build process to make sure that any required files are placed copied to the build location of the Jar

You could...

Place the files within a well know location, for example, on Windows you could use {user.home}/AppData/Local/{application name} or on Mac you could use {user.home}/Library/Application Support/{application name} and place the files here, then you could use an absolute path to the files

This likely becomes an installation issue, as you need to ensure that any required files are copied to the required location when the application is installed.

If the files are auto generated at runtime, then you just need to make sure the directories exists and make them if they don't

You could...

Store the files within the Jar context (AKA embedded resources), the means by which you do this will depend on your IDE and build process, for example, in Netbeans, you can copy files into the src directory of your project and they will automatically be included in the resulting Jar file. I believe that Eclipse can work the same way. However, if you're using Maven, you will need to place the files into the resources directory instead.

You would then access these resources using Class#getResource or Class#getResourceAsStream depending on your needs.

This will make the resources read-only however.

这篇关于我应该在哪里放置我的文件,以便在我运行jar时能够访问它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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