0

I have been searching for 3 days now nonstop looking at every post I can find. My program runs on IntelliJ, but cannot run on an executable. Your help would be appreciated :)

More importantly, where can I find a in depth user-friendly tutorial? Is there a course or book I can pay for? On Udemy, the java classes completely fail to mention I/O such as classpath and URI. TutorialsPoint briefly goes over I/O buts its not indepth. Did I miss something? Is there an easier way to do all this??

Similar posts that have not worked for me: Java Jar file: use resource errors: URI is not hierarchical https://stackoverflow.com/a/27149287/155167

I am trying to load an excel file. I am using Maven. Apache POI says it needs a File. So InputStream does not work. http://poi.apache.org/components/spreadsheet/quick-guide.html#FileInputStream

When I java -jar jarFile, it gives me the error:

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)

Here is the code

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();
    }

If it helps here is the path to the excel file: src\main\resources\excel_List.xlsx

UPDATE: so I took the excel file out of the resources folder \nameOfMyProgram\excel_List.xlsx and now I get this error. I tried several versions of using the classLoader, Class and Thread to solve this error from Different ways of loading a file as an InputStream but I still cannot get it to compile.

Error and my code

Sina A
  • 13
  • 4

2 Answers2

0

If you have to use File object do not put xls-file into resources directory.
Maven puts all files from resources directory into jar.
Java can not create File object based on file in jar-file.
Put your xls-file somewhere in file system and create File object based on its URL.

Since your xls-file is not a resource do not use getResource.
Its URL is its full filename (with path).

Alexandr Ivanov
  • 389
  • 1
  • 5
  • 7
  • Hi. Thank you, but it still does not work. I brought the file out of the resources directory. And now its giving me a FileNOTFoundError error. The URI is set to null. I updated my question above. – Sina A Aug 18 '20 at 20:27
  • Thank you for your help. That helped me get closer to how to solve it. I posted the code below as an answer so it can easily be used. – Sina A Aug 21 '20 at 03:40
0

This code below works with jar executable

String path = new File("excel_List.xlsx").getAbsoluteFile().toString();
            File file = new File(path);
            if(!file.exists()){
                JOptionPane.showMessageDialog(null,"File not found!");
                System.exit(0);
            }
            OPCPackage pkg = OPCPackage.open(file);
            XSSFWorkbook wb = new XSSFWorkbook(pkg);
Sina A
  • 13
  • 4