我正在尝试将文本文件加载为InputStream,但从未拾取txt 文件,并且输入流值始终为null。我不知道为什么会发生这种情况,我想请求帮助。
nohup java -jar crawler-jar-2014.11.0-SNAPSHOT.jar -classpath /home/nbsxlwa/crawler/resources/common-conf:/home/nbsxlwa/crawler/resources/dotcom-conf:./plugins/*:./lib/* &
txt 文件位于 /home/nbsxlwa/crawler/resources/dotcom-conf
目录中。我可以确认该文件确实存在,所以我不知道为什么该文件没有被拾取。设置如下:
`System.getEnvironment("java.class.path")
返回以下内容 值
crawler-jar-2014.11.0-SNAPSHOT.jar
代码块正在尝试用文本创建输入流。
String fileRules = conf.get(URLFILTER_REGEX_FILE); System.out.println("file rules = " + fileRules); // Pass the file as a resource in classpath. // return conf.getConfResourceAsReader(fileRules); // Pass the file as a resource in classpath. InputStream is = RegexURLFilter.class.getResourceAsStream("/" + fileRules); System.out.println("Inputstream is = " + is); System.out.println(ClassLoader.getSystemResourceAsStream(fileRules));
上面代码片段的输出是
file rules = regex-urlfilter.txt
Inputstream is = null
null
- 我尝试将类路径文件夹添加到类路径 MANIFEST.MF 文件中。 MANIFEST.MF 包含以下条目,但输出仍然返回 null。
Class-Path: resources/common-conf resources/dotcom-conf resources/olb-conf lib/gora-cassandra-0.3.jar ** OTHER JARS**
最佳答案
注意 java
man page entry for -jar
When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.
因此您的其他类路径条目将被忽略。您必须使用普通的旧 java
命令,指定一个具有 main 方法的类,并将您的 .jar
包含在类路径中。
您在 MANIFEST 中指定的相对路径是相对于 JAR 本身的,或者是完整的 URL。
关于Java 二进制文件未从指定的类路径获取资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25484558/