将非 jar 文件添加到 Java 类路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6764620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 17:10:16  来源:igfitidea点击:

Adding a non-jar file to the Java class path

javaclasspath

提问by Ignatius

I'm trying to make a .txt file available to my application via the class path. In my startup script--which is co-located in the same folder as the .txt file--I've set the following:

我正在尝试通过类路径为我的应用程序提供一个 .txt 文件。在我的启动脚本中——它与 .txt 文件位于同一文件夹中——我设置了以下内容:

set CLASSPATH=%CLASSPATH%;%CD%\sample.txt java -classpath %CD%\sample.txt

set CLASSPATH=%CLASSPATH%;%CD%\sample.txt java -classpath %CD%\sample.txt

In my application, I've tried the following:

在我的应用程序中,我尝试了以下操作:

  1. getClass().getResource("sample.txt")
  2. getClass().getResource("/sample.txt")
  3. getClass().getResource("classpath:sample.txt")
  1. getClass().getResource("sample.txt")
  2. getClass().getResource("/sample.txt")
  3. getClass().getResource("classpath:sample.txt")

None of the above work. Any help would be appreciated.

以上都不起作用。任何帮助,将不胜感激。

回答by Alex Gitelman

You must pack you txt file inside jar or place it in directory included in classpath.

您必须将 txt 文件打包到 jar 中或将其放在类路径中包含的目录中。

回答by Eli Acherkan

You should add to your classpath the directorycontaining the file, and not the file itself:

您应该将包含文件的目录添加到类路径中,而不是文件本身:

set CLASSPATH=%CLASSPATH%;%CD%