How to Open Any Folder as a Project in the NetBeans Platform

Typically, as described in the NetBeans Project Type Tutorial, you'll define a project type based on the presence of a file (e.g., "project.xml" or "customer.txt" or something like that) in a folder. I.e., if the file is there, then its parent, i.e., the folder that contains the file, is a project and should be opened in your application.

However, in some scenarios (as with the HTML5 project type introduced in NetBeans IDE 7.3), the user should be able to open absolutely any folder at all into the application. How to create a project type that is that liberal?

Here you go, the only condition that needs to be true is that the selected item in the "Open Project" dialog is a folder, as defined in the "isProject" method below. Nothing else. That's it. If you select a folder, it will be opened in your application, displaying absolutely everything as-is (since below there's no ProjectLogicalView defined):

import java.beans.PropertyChangeListener;
import javax.swing.Icon;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectInformation;
import org.netbeans.spi.project.ProjectFactory;
import org.netbeans.spi.project.ProjectState;
import org.openide.filesystems.FileObject;
import org.openide.loaders.DataFolder;
import org.openide.loaders.DataObjectNotFoundException;
import org.openide.nodes.FilterNode;
import org.openide.util.Exceptions;
import org.openide.util.ImageUtilities;
import org.openide.util.Lookup;
import org.openide.util.lookup.Lookups;
import org.openide.util.lookup.ServiceProvider;

@ServiceProvider(service = ProjectFactory.class)
public class FolderProjectFactory implements ProjectFactory {
    public boolean isProject(FileObject projectDirectory) {
        return DataFolder.findFolder(projectDirectory) != null;
    public Project loadProject(FileObject dir, ProjectState state) throws IOException {
        return isProject(dir) ? new FolderProject(dir) : null;
    public void saveProject(Project prjct) throws IOException, ClassCastException {
        // leave unimplemented for the moment
    private class FolderProject implements Project {
        private final FileObject projectDir;
        private Lookup lkp;
        private FolderProject(FileObject dir) {
            this.projectDir = dir;
        public FileObject getProjectDirectory() {
            return projectDir;
        public Lookup getLookup() {
            if (lkp == null) {
                lkp = Lookups.fixed(new Object[]{
                    new Info(),
            return lkp;
        private final class Info implements ProjectInformation {
            public Icon getIcon() {
                Icon icon = null;
                try {
                    icon = ImageUtilities.image2Icon(
                            new FilterNode(DataFolder.find(
                } catch (DataObjectNotFoundException ex) {
                return icon;
            public String getName() {
                return getProjectDirectory().getName();
            public String getDisplayName() {
                return getName();
            public void addPropertyChangeListener(PropertyChangeListener pcl) {
                //do nothing, won't change
            public void removePropertyChangeListener(PropertyChangeListener pcl) {
                //do nothing, won't change
            public Project getProject() {
                return FolderProject.this;

Even the ProjectInformation implementation really isn't needed at all, since it provides nothing more than the icon in the "Open Project" dialog, the rest (i.e., the display name in the "Open Project" dialog) is provided by default regardless of whether you have a ProjectInformation implementation or not.


There's a little more to it to make something with a good user experience - the code above makes the IDE recognize *absolutely every* folder as a project.

That's going to break functionality like Ctrl-Shift-1 to show a file in the Projects tab, and probably refactoring as well, for all projects in the IDE - anything that relies on finding the project that owns a Java source file in order to do something will find one of these projects instead.

Posted by Tim Boudreau on June 22, 2013 at 08:49 AM PDT #

This is not for usage in the context of NetBeans IDE. This is for any project that uses the NetBeans Platform as a basis that would like to be able to open projects, i.e., you're not going to have Java source files, and refactoring, and Ctrl-Shift-1, in that scenario to worry about.

Posted by Geertjan on June 22, 2013 at 10:21 AM PDT #

Hi, I am new to NetBeans IDE. I want to import a Fortran project into NetBeans 8.0.1. The project was developed in Fortran 2008. I have tried to import all the files as zip but it did not import. Then I manually added all the files and folders in the project. When I compile it, its shows error that "Can't open module file 'moduledefs.mod' for reading at (1): No such file or directory nbproject"

Is there any solution?

Posted by guest on October 10, 2014 at 01:50 PM PDT #

Can you point to the URL where it says NetBeans supports Fortran?

Posted by Geertjan on October 10, 2014 at 02:13 PM PDT #


I want to use your above code. I am new to NetBeans. Could you tell me how to use to above code in NetBeans to make it work?


Posted by Tamil Vendhan Kanagaraju on October 29, 2014 at 04:48 AM PDT #

This is a pretty old blog entry. In NetBeans IDE 8 or 8.0.1, simply use the "HTML5 Application" project type in NetBeans IDE -- it does the same thing as the above.

Posted by Geertjan on October 29, 2014 at 04:51 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.


« July 2016