[英]Apache CLI, Executable jar, classLoader().getResource()
My goal is to use Apache CLI with an executable jar file to read in a text file, perform string manipulations, and then write to a CSV. 我的目标是将Apache CLI与可执行的jar文件一起使用以读取文本文件,执行字符串操作,然后写入CSV。 You would execute the tool in the terminal like this:
您可以像这样在终端中执行该工具:
$ java -jar my-tool-with-dependencies.jar -i input.txt -o output.csv
I've written tests for this functionality and those tests are passing. 我已经为此功能编写了测试,并且这些测试都通过了。 The test input text file is located in
src/test/resources/
. 测试输入文本文件位于
src/test/resources/
。 The following test is passing: 通过了以下测试:
@Test
public void testWordockerWriteCsvFileContents() {
// Make sure the csv file contains contents
Wordocker w = new Wordocker();
String intext = "/textformat/example_format.txt";
String outcsv = "/tmp/foo.csv";
w.writeCsvFile(intext, outcsv);
try {
Reader in = new FileReader(outcsv);
Iterable<CSVRecord> records = CSVFormat.DEFAULT.parse(in);
for (CSVRecord record : records) {
assertTrue(record.toString().length() > 0);
}
} catch(FileNotFoundException e){
assertTrue(false);
} catch(IOException e) {
assertTrue(false);
}
File file = new File(outcsv);
if (file.exists()) {
file.delete();
}
}
We I compile my jar files with dependencies using mvn clean compile assembly:single
then I raise the following FileNotFoundException: 我们使用
mvn clean compile assembly:single
具有依赖项的jar文件,然后引发以下FileNotFoundException:
// Get file from resources folder
URL resourceURL = ParseDoc.class.getClassLoader().getResource(fileName);
if (resourceURL == null) {
throw new FileNotFoundException(fileName + " not found");
}
file = new File(resourceURL.getFile());
This leads me to believe that there is an issue with where ParseDoc.class.getClassLoader().getResource(fileName);
这使我相信
ParseDoc.class.getClassLoader().getResource(fileName);
那里存在问题ParseDoc.class.getClassLoader().getResource(fileName);
is looking for the file. 正在寻找档案。 I'm aware of related questions which have been asked.
我知道有人问过相关问题。 Related questions are the following:
相关问题如下:
None of these questions appear to ask about how to use an executable jar with Apache CLI. 这些问题似乎都没有询问如何在Apache CLI中使用可执行jar。 I think the basic issue is that the filepath given by my command line argument cannot be found by
URL resourceURL = ParseDoc.class.getClassLoader().getResource(fileName);
我认为基本问题是无法通过
URL resourceURL = ParseDoc.class.getClassLoader().getResource(fileName);
找到我的命令行参数给定的文件路径URL resourceURL = ParseDoc.class.getClassLoader().getResource(fileName);
. 。
Please let me know what you think. 请让我知道你的想法。 Thank you for your time.
感谢您的时间。
I'm posting this as answer as well after discussion via comments: 在通过评论讨论之后,我也将其发布为答案:
Classloader.getResource()
is only fetching files that are packaged as part of the Jar-file or located in the classpath-folders. Classloader.getResource()
仅获取打包为Jar文件的一部分或位于classpath文件夹中的文件。
For reading a normal file you would use something like your first example, ie FileReader
or FileInputStream
or simply pass a java.io.File
depending on what the library that you are trying to use supports. 为了读取普通文件,您将使用类似于第一个示例的内容,即
FileReader
或FileInputStream
或根据要使用的库所支持的内容简单地传递java.io.File
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.