Free Support Forum -

Can't figure out Password Protected Search working in Java

Hi !
I want to search for a keyword in all the documents existing in my dictionary, whether protected or not.
For, the protected ones, I tried using the sample code for the same. Here is the code that I wrote :

public static void searchPasswordProtectedDocument() {
// Creating index
Index index = new Index(Utilities.INDEX_PATH);
// Sets passwords for documents
index.getDictionaries().getDocumentPasswords().add(Utilities.PASSWORD_PROTECTED_DOCUMENT, “test”);
// Indexing
index.addToIndex(Utilities.DOCUMENTS_PATH, true);
// Searching
SearchResults results =“sample”);
// Print results in the console
for (DocumentResultInfo result : results) {
System.out.println(result.getHitCount() + " hits are in " + result.getFileName());

Can you guys , please help me understand how this function works ?
// Sets passwords for documents
index.getDictionaries().getDocumentPasswords().add(Utilities.PASSWORD_PROTECTED_DOCUMENT, “test”);
In this line, what it does ? Are we giving password to it to open files, or is it setting passwords? And, the path for the files. HereUtilities.PASSWORD_PROTECTED_DOCUMENT, what value it holds, where should this refer ?

Please help me understand this !
Thanks !


In the given code snippet, this function is taking two parameters.

  • Password protected document
  • Password of such a document

Hence, PASSWORD_PROTECTED_DOCUMENT is set to document path and “test” is actually password of the protected/secured document.
You can see what path is assigned to PASSWORD_PROTECTED_DOCUMENT in class:

public static final String PASSWORD_PROTECTED_DOCUMENT = System.getProperty("user.dir") + "\\Data\\Documents\\Password Protected Document.docx";

Thanks !
But, does that mean,

  • I can only search for one file at a time, if it’s protected ?
  • I also need to know the password for that file necessarily, right ?

If it’s so, then how will search for any word/term and get the list of all the documents that has that word. Because, simple search scans all the files except the ones which are protected.
What in such case ?


No. Your search process operates over a folder of documents. Now, this folder could possess different types of documents (password protected or simple).

Yes, if you are going to add a password protected document to the directory, you have to specify the password.

index.getDictionaries().getDocumentPasswords().add(Utilities.PASSWORD_PROTECTED_DOCUMENT, "test");

We searched for a word that was in multiple files including a password protected document and we got this output.PNG (41.1 KB).
Hence, you can get search results from all type of documents. But again, when you add a password protected document to the documents folder, you have to update the index and then perform search operations.

That’s okay.
But, what in case of my question ?
If I have more than one protected files in my directory , and considering normal scenario, not all the files would be having the same password. So, then we would have to provide passwords for each protected file. How’s that possible then? This leads the first limitation that I doubted, that I can only search for a single password protected document. Right ?

1 Like


Yes, you can add as much password protected docs as you want in the directory and perform search over it.
This is how multiple password protected files could be added:

index.getDictionaries().getDocumentPasswords().add(Utilities.PASSWORD_PROTECTED_DOCUMENT, "test"); index.getDictionaries().getDocumentPasswords().add(Utilities.PASSWORD_PROTECTED_DOCUMENT2, "test123");