Getting all .java files within a project

1) Currently, I'm resorting to an auxiliary map to get all .java files in a project:

final Map<String, VirtualFile> locationMap =
Maps.newHashMap(); // The inner does not have to be concurrent.
ProjectFileIndex.SERVICE.getInstance(project).iterateContent(new ContentIterator() {
public boolean processFile(VirtualFile fileOrDir) {
String cloudPathIndex = getCloudPathFromFile(project, fileOrDir);
if (!Strings.isNullOrEmpty(cloudPathIndex)) {
locationMap.put(cloudPathIndex, fileOrDir);
return true;


Ideally, such a map would already be built and I wouldn't need to perform this traversal myself. Is there an alternative to using ProjectFileIndex in this generic way?

2) Is there any class that gives me VirtualFiles indexed in a similar way to <package_name>[/.]<file_name>?





Thanks for your response.

I modified your code to give me just the files in my project:

FileBasedIndex.getInstance().getContainingFiles(FileTypeIndex.NAME, JavaFileType.INSTANCE, GlobalSearchScope.projectScope(project))

But the files are still indexed by their absolute path. e.g., file:///Users/joaomartins/IdeaProjects/JoaoDiscoProject3/jamjavatest/guestbook/src/test/java/ninja/joao/guestbook/

I'd like files to be indexed by ninja/joao/guestbook/ (package/, instead.

I haven't found any class that does this. Am I to understand no class offers this?


if you are looking for path relative to src, you could replace path up to src with "".

here is how to get src:



What are you trying to accomplish? Collecting all .java files in a project in a single collection is unlikely to be an efficient way to solve your problem.


JavaPsiFacade.findClass(qualifiedName, project.getProjectScope).getVirtualFile()


Hello Dmitry,

let's say we'd like to build cache of Psi elements.

if getting a collection of all virtual files is not good, what is the recommended way of accessing all Psi elements in a project?

is this possible to access cache built by Idea?


Yes, of course it's possible. For example, you can use classes like JavaClassNameIndex and JavaFieldNameIndex.


I want to register a breakpoint on a file, but the breakpoint location can vary.

If I were to standardize breakpoint locations on something like com/package/name/ (which is what you'd get from an agent that only know the class it's on right now), I would want a ServerToIDEFileResolver that, given a key like the full class name before, gives me the VirtualFile. That resolver would only need to resort to that collection once, and then whenever files are added/deleted.

Using the IDEA platform, I've been able to get all .java files for a project but indexed by their absolute path in the file system.

I will have a go at the JavaPsiFacade you mentioned. Thanks!


> you can use classes like JavaClassNameIndex and JavaFieldNameIndex.

.. and for custom languages, does Idea build cache for those too? Or are only built-in / supported languages being cached?


For a custom language, you can build your own indices with IDEA's help.


Thank you very much! I was not aware of FileBasedIndex

Cheers Dmitry


I'm finding unit testing FileBasedIndex a real headache.


I've got this piece of code:

Collection<VirtualFile> projectJavaFiles = ideaFileIndex.getContainingFiles(
FileTypeIndex.NAME, JavaFileType.INSTANCE, GlobalSearchScope.projectScope(project));
for (VirtualFile virtualFile : projectJavaFiles) {

I create my project like:

project = PlatformTestCase.createProject("/full/fs/path", "creation_place");

And then try to add files in many different ways:

project.getBaseDir(), "path/to/prj/src/main/com/java/package/");
project.getBaseDir().createChildData(this, "path/to/prj/src/main/com/java/package/");
PlatformTestCase.addSurvivingFiles(ImmutableList.of(javaFile1, javaFile2));

However, project.getBaseDir() returns null.

What is the recommended way to add files to a test project?

Also, is it required that files are actually written to the file system? Reading the PlatformTestCase code, it seems that way, although I shouldn't need to write anything to disk to do unit testing in this case...


Thanks for your help.


Thanks, that shed some light into unit testing in IDEA.

There is one thing that's puzzling me: in my code, one of the ways I'm fetching files is

VirtualFile file = fileSystem.findFileByPath(project.getBasePath() + "/" + path);

In my unit test, I'm adding a test file as

file1 = this.myFixture.addFileToProject(
"path/to/prj/src/main/com/java/package/", "");

The path this file is added to is


whereas project.getBaseDir() returns


... and the fileSystem fetch fails. This should work out of the box, without me having to hack anything for the files to have the same path. It is working when I run the plugin normally.

Do you have any suggestions for how to resolve this?



Please sign in to leave a comment.