简体   繁体   中英

.xls file not detected java

When I try to load the xls file it doesn't work even though is in the same folder. I also tried it with absolute paths. (It all happened because of missing jars, here is the list with all of them. To solve the issue of relative paths the url.getResource() from below works fine.) My Jar List(Image)

public class Main {
    
        public static void main(String[] args) throws FileNotFoundException, IOException {
            FileInputStream f = new FileInputStream("MP.xls");
            HSSFWorkbook l = new HSSFWorkbook(f);
        }
        
    }
 public class Main {
    
        public static void main(String[] args) throws FileNotFoundException, IOException {
            FileInputStream f = new FileInputStream("C:\\Users\\alumno.Alumno-PC\\Documents\\NetBeansProjects\\pruebas xlsx\\src\\pruebasxlsx\\MP.xls");
            HSSFWorkbook l = new HSSFWorkbook(f);
        }
        
    }

This is where the file is located(picture)

This is the error with relative path:

Exception in thread "main" java.io.FileNotFoundException: MP.xls (El sistema no puede encontrar el archivo especificado)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:216)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:111)
at pruebasxlsx.Main.main(Main.java:22)

this is the error with an absolute path

   Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/math3/util/ArithmeticUtils
    at org.apache.poi.poifs.property.RootProperty.setSize(RootProperty.java:59)
    at org.apache.poi.poifs.property.DirectoryProperty.<init>(DirectoryProperty.java:52)
    at org.apache.poi.poifs.property.RootProperty.<init>(RootProperty.java:31)
    at org.apache.poi.poifs.property.PropertyTable.<init>(PropertyTable.java:58)
    at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:99)
    at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:272)
    at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:399)
    at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:381)
    at pruebasxlsx.Main.main(Main.java:24)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.math3.util.ArithmeticUtils
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
    ... 9 more

File input stream gets the file named by the path name in the file system - that means an absolute path, as parameter, not relative path.

If you want to load a file from the location of your class use:

URL url = class.getResource("file.txt");

and then get absolute path of it by using:

url.getPath();

So, below solution should work (it can be polished but you will get an idea):

public class Main {
   static URL url = Main.class.getResource("MP.xls");

   public static void main(String[] args) throws FileNotFoundException, IOException {
       FileInputStream f = new FileInputStream(url.getPath());
       HSSFWorkbook l = new HSSFWorkbook(f);
    }
}

The FileInputStream with the absolute path works. Just the reading with HSSFWorkbook- misses a library.

The relative path is a volatile option. If the file is a read-only source, bundle it with the application, and do not use a File (file system file), but use a resource, a "file" on the class path, possibly bundled in an application jar.

   InputStream f = Main.class.getResourceAsStream("/pruebasxslx/MP.xls");
   HSSFWorkbook l = new HSSFWorkbook(f);

For HSSFWorkbook a library with org.apache.commons.math3 classes is not found, an indirect needed library.

As these library dependencies are extra work, already done by others, and involving moving coherent versions of all libraries to work together, one should better use for instance maven , a build infrastructure. Maven (or gradle) projects are supported by most IDEs, and have a bit different directory structure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM