Working with Remote Files in Netbeans

by on July 30th, 2009
No Gravatar

If you’re like me and you develop applications for the Linux platform from a remote computer, then you may find the following tutorial interesting.

For a long time jEdit was the perfect editor for me. It has syntax highlighting for just about every language and it has an excellent (S)FTP file browser. What more could you need as a PHP developer? Apparently a lot. With the maturation of PHP development came IDEs which provide code completion, inline documentation, a class browser, SCM integration, fonts that are prettier than jEdit’s and so much more. The PHP IDE that has stood out the most to me is Netbeans.

If you’ve used Netbeans before and tried to connect to your remote machine with a Samba share, or some other way of mounting a remote file system you’ve likely encountered performance issues. Netbeans needs to index all your code in order to provide all its features; scanning files via a remote filesystem is too slow. In my case, scanning code from the remote machine took over an hour and even once it was done scanning Netbeans would lock up from time to time!

So how do we overcome this limitation? By maintaining a copy of your code on your local machine. This may seem strange since the code is never executed on your machine, but it is necessary. Don’t worry, Netbeans makes synchronizing your local machine with your remote machine transparent.

The Process

  • If you haven’t already, download Netbeans and install it.
  • Start Netbeans.
  • Create a new Netbeans project: click File->New Project.
  • Choose the PHP Category, choose PHP Application, then click Next.
  • Enter a name for your project.
  • The Sources Folder is the location that files will be stored on your local machine. Choose a folder/directory that suits your needs.
  • Click Next.
  • For Run As choose Remote Website (FTP, SFTP).
  • For Project URL specify whatever URL your application is found at, i.e. http://tutorial.com/ .
  • To specify the SFTP configuration click Manage. This is where you configure your remote machines. To add a new remote machine click Add….
  • Choose a Connection Name, a good one would be the name of the remote machine.
  • Make sure to change Connection Type to SFTP.
  • Click OK.
  • Enter your SFTP connection details. If you’re on Windows you’ll likely have to create an empty file to use for the Known Hosts File. I created a file named “C:\Users\myusername\known_hosts”. Linux and OS X users should specify their existing known_hosts usually found in ~/.ssh/known_hosts.
  • Set Initial Directory to a parent directory of where your code resides. In my case the remote machine hosts many sites which reside in “/var/www/docroot/” and that’s what I specify for the Initial Directory.
  • Click Test to verify your settings and if all is well click OK.
  • For Remote Connection select the connection you just created.
  • If your code is located directly in the Initial Directory then leave Upload Directory blank. If your code is located in a sub directory of the Initial Directory such as “/var/www/docroot/tutorial.com”, then specify “/tutorial.com” for Upload Directory.
  • Set Upload Files to On Save.
  • Click Finish.

Important Note

By default Netbeans creates an index.php file in new projects. If you delete it from the Netbeans project you will also be deleting it from your remote machine if one exists! This is because from here on any changes you make in your Netbeans project (edit/save, delete, rename, etc…) will automatically be propagated to your remote machine!

What Next?

If the directory on the remote machine you specified when setting up the project already has files you need to download them. You should see a list of your open projects including the one we’ve just created. Right click Source Files found under your new project and click Download. You will be asked to confirm which files to download.

If there are any libraries or classes your project requires you should copy them to your local machine as well. To add libraries to your project right click your project, click Properties, choose PHP Include Path and click Add Folder.

Happy coding!

8 Responses to “Working with Remote Files in Netbeans”

  1. JonasNo Gravatar says:

    Well theres a flaw in this.

    If other people besides you are working on the project and changes a file, you (AFAIK) have no indication of such change being made, making it very likely you will overwrite any changes others made to the PHP project.

    Is there a way to fix this?

  2. Jonathan DartNo Gravatar says:

    As far as I know you are correct. You can always download the latest file before you start editing by right clicking it and choosing download, however this might not be your ideal.

    Consider making a feature request.

    In my experience when working in a team it’s generally better to use a version control system to manage changes.

  3. AnonNo Gravatar says:

    This article isn’t about working with remote files. It’s about working with local files and using SFTP to then upload them to the server.

    How do we work directly with the server files?

  4. NetWorksNo Gravatar says:

    Yes this article is about setting up a project with a remote server. As this is a pretty feature it sucks when you work with more than one PC/Mac on a project. I just don´t know what netbeans is doing :-)
    When I start it, Netbeans is logging in and that scares me. So I always download changed Files before i open Netbeans.
    I won´t setup a cvs for every website and it would be pretty nice to know HOW netbeans handle files. Will it upload older files/download newer files? Is there a way to set up server time? Check in/out would be so nice. I hope I´ll find an addon for this.
    The NB-developer should make it work like “espresso”: When I open a file it will be downloaded first from the server and upload it on save. That means when I choose existing sources the local project is the newest one and when I choose remote files the remote server is the newest one.
    World could be so easy.

  5. AshishNo Gravatar says:

    I have done all the task for the remote connection and I have all the code in local directory.
    Now my problem is whenever I download file It show me read-only mode.
    So is there any solution that I can edit that file after download file.

    Please any have idea for this little problem in Netbeans 7.0

    Thanks in advance…

Leave a Reply