简体   繁体   English

如何使Java查看文件的特定位置

[英]How to get Java to look at a specific location for a file

I'm creating a Jar file which automatically creates a target folder. 我正在创建一个Jar文件,该文件会自动创建目标文件夹。

Within the the target folder I have an XSD file. 在目标文件夹中,我有一个XSD文件。 When I execute the jar file via the command prompt - it is looking for the file in the target folder as opposed to the test-classes folder. 当我通过命令提示符执行jar文件时-它正在目标文件夹中而不是test-classes文件夹中寻找文件。

Is there anyway I can configure this so that the compiler looks at the test-classes folder for the file? 无论如何,我可以对其进行配置,以便编译器查看文件的test-classes文件夹吗?

Thanks 谢谢

The "test-classes folder" is just a random place on your hard disk. “ test-classes文件夹”只是硬盘上的随机位置。

Java supports 3 generic places to look for resources: Java支持3个通用位置来查找资源:

  1. the current working directory . 当前工作目录 this is where your command prompt is. 这是您的命令提示符所在的位置。

  2. the user home This is what you get when calling System.getProperty("user.home") . 用户主目录这是调用System.getProperty("user.home") On Unix this resoves to $HOME and on Windows to %USERPROFILE% 在Unix上,此结果存储到$HOME ,在Windows上,它存储到%USERPROFILE%

  3. the own code location . 自己的代码位置 Resources in the same package as your class are accessed with getClass().getResource("filenameWithoutPath") But usually you place resources in a special folder in the application root and access it like this: getClass().getResource("/relative/path/from/src/root/filenameWithoutPath") 使用getClass().getResource("filenameWithoutPath")访问与您的类相同的包中的资源,但是通常您将资源放在应用程序根目录中的特殊文件夹中,并按以下方式进行访问: getClass().getResource("/relative/path/from/src/root/filenameWithoutPath")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM