【发布时间】:2020-08-13 00:49:09
【问题描述】:
我已经搜索了 3 天,现在不停地查看我能找到的每个帖子。我的程序在 IntelliJ 上运行,但无法在可执行文件上运行。您的帮助将不胜感激:)
更重要的是,我在哪里可以找到深入的用户友好教程?有我可以支付的课程或书籍吗?在 Udemy 上,java 类完全没有提到类路径和 URI 等 I/O。 TutorialsPoint 简要介绍了 I/O,但并不深入。我错过了什么?有没有更简单的方法来完成这一切??
对我不起作用的类似帖子: Java Jar file: use resource errors: URI is not hierarchical https://stackoverflow.com/a/27149287/155167
我正在尝试加载一个 Excel 文件。我正在使用 Maven。 Apache POI 说它需要一个文件。所以 InputStream 不起作用。 http://poi.apache.org/components/spreadsheet/quick-guide.html#FileInputStream
当我使用 java -jar jarFile 时,它给了我错误:
C:\Users\1010\Documents\Personal\MonsterManager>java -jar monsterManagerVer3.jar
Exception in thread "main" java.lang.IllegalArgumentException: URI is not hierarchical
at java.base/java.io.File.<init>(File.java:421)
at LoadExcel.<init>(LoadExcel.java:62)
at monsterRunner.<init>(monsterRunner.java:13)
at monsterRunner.main(monsterRunner.java:24)
这里是代码
public LoadExcel() throws IOException, URISyntaxException, InvalidFormatException {
mNames = null;
URL ins = this.getClass().getResource("/excel_List.xlsx");
if (ins == null)
throw new FileNotFoundException("The XLSX file didn't exist on the classpath");
ins.toString().replace(" ","%20"); //<-Runs without this part
File file = new File(ins.toURI()); //this is line 62
// File f = new File(getClass().getResource("/MyResource").toExternalForm());
//String path = this.getClass().getClassLoader().getResource("/excel_List.xlsx").toExternalForm();
//File file = new File(path);
OPCPackage pkg = OPCPackage.open(file);
XSSFWorkbook wb = new XSSFWorkbook(pkg);
//FileInputStream fis=new FileInputStream(new File(excelFile));
// XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet = wb.getSheetAt(0);
loadExcel(sheet);
cacheNames();
// fis.close();
wb.close();
}
如果有帮助,这里是 excel 文件的路径: src\main\resources\excel_List.xlsx
更新: 所以我把excel文件从资源文件夹中取出 \nameOfMyProgram\excel_List.xlsx 现在我得到了这个错误。 我尝试了使用类加载器、类和线程的几个版本来解决来自Different ways of loading a file as an InputStream 的这个错误 但我仍然无法编译。
【问题讨论】:
标签: maven io apache-poi uri executable-jar