java -jar ./target/jar vs java -jar ./jar

huangapple go评论67阅读模式
英文:

java -jar ./target/jar vs java -jar ./jar

问题

以下是翻译好的内容:

在主题中提到,项目文件夹内运行JAR文件与target文件夹内运行JAR文件的区别是什么,以及这如何影响从类路径中的“resource”文件夹读取数据?

是否有一种方法可以根据我们从哪个文件夹运行JAR文件来获取到“resource”文件夹的路径?

英文:

As in topic, what's the difference between running jar within project folder and within target folder and how this can affect reading data from resource folder within classpath?

Is there any way to fetch path to the resource folder depends on from which folder we running jar?

答案1

得分: 3

你的 JAR 文件的位置不应影响类路径。因此,如果你的 resource 文件夹被打包在 JAR 文件中,在两种情况下你都可以以相同的方式访问它。

英文:

The location of your JAR file should have no impact on the classpath. So if your resource folder is packaged in the jar, you access it in the same way in both cases.

答案2

得分: 1

在大多数情况下,打包为可执行JAR文件的应用程序将包含其所需的所有基于文件的资源。应用程序代码将使用类加载器或某些资源管理框架来查找这些资源。

然而,绝对没有任何阻止您编写一个从真实文件系统中读取内容的应用程序,并将该应用程序打包为JAR文件。如果这样做,尽管JAR文件本身的文件系统位置不应产生任何差异,但当前工作目录很可能会产生影响。

英文:

In most cases, an application packaged as an executable JAR will contain all the file-based resources it needs. The application code will use, for example, the class loader, or some resource management framework, to find those resources.

However, there's absolutely nothing to stop you coding an application that reads from the real filesystem, and packaging that application as a JAR. If that is done then, although the filesystem location of the JAR itself shouldn't make any difference, the current working directory very likely will.

huangapple
  • 本文由 发表于 2020年9月10日 19:46:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63829015.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定