我应该把我的文件放在哪里以便在我运行 jar 时能够访问它们?

Where should i place my files in order to be able to access them when i run the jar?(我应该把我的文件放在哪里以便在我运行 jar 时能够访问它们?)
本文介绍了我应该把我的文件放在哪里以便在我运行 jar 时能够访问它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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...

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

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...

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

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

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

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

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

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.

然后您可以使用 Class#getResourceClass#getResourceAsStream 取决于你的需要.

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

这将使资源只读.

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

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to implement RecyclerView with section header depending on category?(如何根据类别实现带有节标题的 RecyclerView?)
How to generate JNI header file in Eclipse(如何在 Eclipse 中生成 JNI 头文件)
Setting a custom HTTP header dynamically with Spring-WS client(使用 Spring-WS 客户端动态设置自定义 HTTP 标头)
Could you technically call the string[] anything in the main method?(从技术上讲,您可以在 main 方法中调用 string[] 吗?)
What is the proper way of setting headers in a URLConnection?(在 URLConnection 中设置标头的正确方法是什么?)
How to overwrite http-header quot;Hostquot; in a HttpURLConnection?(如何覆盖 http-header “主机在 HttpURLConnection 中?)