Adding a non-jar file to the Java class path

喜夏-厌秋 提交于 2019-11-29 20:24:04

问题


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:

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")

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


回答1:


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




回答2:


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

set CLASSPATH=%CLASSPATH%;%CD%


来源:https://stackoverflow.com/questions/6764620/adding-a-non-jar-file-to-the-java-class-path

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!