Catch if a file is password protected

While reading an excel file as follows, if the file is password-protected, it gives the following exception:

Workbook wb = new Workbook(“E:/Excel_password_protected.xlsx”);

Stack trace:

java.lang.NoClassDefFoundError: org/bouncycastle/jce/provider/BouncyCastleProvider
at com.aspose.cells.zub.a(Unknown Source)
at com.aspose.cells.zub.a(Unknown Source)
at com.aspose.cells.zti.a(Unknown Source)
at com.aspose.cells.zcak.a(Unknown Source)
at com.aspose.cells.Workbook.(Unknown Source)
at com.birst.file.parser.ReadPasswordExcel.main(ReadPasswordExcel.java:9)
Caused by: java.lang.ClassNotFoundException: org.bouncycastle.jce.provider.BouncyCastleProvider
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
… 6 more


We can catch the exception as follows:

try {
Workbook wb = new Workbook(“E:/Excel_password_protected.xlsx”);
} catch (NoClassDefFoundError e) {
System.out.println(“Catching password protected file.”);
e.printStackTrace();
}

This does NOT seem to be an ideal solution to catch the password-protected files.

Would you suggest how to catch the password protected files?

Attached a password-protected excel file for your reference.

Hi,


Thanks for providing us template file, sample code and details.

Well, you may determine if a file is encrypted (password protected) or not beforehand. See the document for your reference:

Thank you.

I am using aspose-cells-java 17.1.0.
Still getting the same error:

public static void main(String[] args) throws Exception {
FileFormatInfo info = FileFormatUtil.detectFileFormat(“E:/codeRelated/password-excel/Excel_password_protected.xlsx”);
System.out.println("The spreadsheet format is: " + FileFormatUtil.loadFormatToExtension(info.getLoadFormat()));
System.out.println("The file is encrypted: " + info.isEncrypted());
}


Exception in thread “main” java.lang.NoClassDefFoundError: org/bouncycastle/jce/provider/BouncyCastleProvider
at com.aspose.cells.zub.a(Unknown Source)
at com.aspose.cells.zub.a(Unknown Source)
at com.aspose.cells.zti.a(Unknown Source)
at com.aspose.cells.FileFormatUtil.a(Unknown Source)
at com.aspose.cells.FileFormatUtil.detectFileFormat(Unknown Source)
at com.birst.file.parser.ReadPasswordExcel.main(ReadPasswordExcel.java:19)
Caused by: java.lang.ClassNotFoundException: org.bouncycastle.jce.provider.BouncyCastleProvider
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
… 6 more

Hi,


Well, you need to first add reference to the following library (JAR) into your classpath as well:
bcprov-jdk16-146.jar
This jar file is required when you use any file that has AES encryption (supported by MS Excel 2010 or greater versions). You can get this library by downloading/extracting any of our newer versions, e.g Aspose.Cells for Java 17.3.0 (latest).

Let us know if you still have any issue.

Thank you.