<

Go Premium for a chance to win a PS4. Enter to Win

x

Beginning Java Programming: Import Projects to Eclipse IDE

Published on
19,605 Points
8,505 Views
6 Endorsements
Last Modified:
Awarded
Kevin Cross
Father, husband and general problem solver who loves coding SQL, C#, Salesforce Apex or whatever.
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench.


Introduction

While learning Java can be done with a simple text editor like gedit or notepad, occasionally the use of an Integrated Development Environment (IDE) is desired.  Eclipse is one such IDE that is used by a fair amount of developers given it is open source, providing a free tool, coupled with the fact that it has origins from IBM's commercial package, WebSphere Studio Application Developer, which adds power amplified by the community additions since its inception.

With all that said, the premise of this article is that we have an existing Java project that must now be developed further within Eclipse.  Therefore, to get the most benefit, Eclipse (screen shots and instructions shown from version 3.5.x or "Galileo") should be installed as well as a JDK.  We will also need a simple project to work with, but we will create one shortly.


Simple Project

1. Create a folder in a temporary directory of your choice and name it "TestProject".  
2. Open the TestProject folder and create a sub directory called "src".
3. Open the newly created src directory and create a sub directory called "testpackage".
4. Open the testpackage folder and create a text file named "TestClass.java" with the following content:
package testpackage;

public class TestClass {
	
}

Open in new window


Challenge: For those really new to Java programming, give the standard "Hello World" style program a whirl. Add an appropriate main method and the code to print greetings on the standard output. As I said, simple!

Now let's do something productive with this meaningful code base, in need of some minor tweaking, in Eclipse. We will look at the more common methods of using Eclipse for this 'project'.


1. New Project from existing source files.



In short, this method allows you to bring an entire project folder into your workspace in three short steps.  

Typically I use this approach for existing project directories in my physical file system workspace but is not yet present in my Eclipse workbench.

1. Launch Eclipse.

2. Create New Java Project.
Click File | New | Java Project (from Java perspective)
        OR
Click File | New | Project, navigate to and choose Java Project.
 Select New Project Type DialogClick Next.

3. Complete New Java Project Dialog.
Click "Create project from existing source" radio button.
 New Project DialogClick browse and navigate to the TestProject directory (you don't need to open, just highlight the folder) then click OK.  

Challenge: explore for yourself having different JRE or project layouts (i.e., what if your Java files were not in a folder called src or even in root of your project folder).

Click Finish and enjoy!


2. Import File System Resources: add source files to a project already in workbench.



This is useful when you have files in a different workspace or other application that you would like to reuse in some fashion in a new project.  If the file existed in your workspace, it is easy enough to copy and paste or refactor | move files or update build path / source folders without having to go through additional processes.

1. Launch Eclipse.

2. Create New Java Project.
Click File | New | Java Project (from Java perspective)
        OR
Click File | New | Project, navigate to and choose Java Project.
 Select New Project Type DialogClick Next.

3. Complete New Java Project Dialog.
Type in project name "TestProject".
Click "Create new project in workspace" radio button.
Click Finish.

4. Import File System Resource.
Expand the TestProject in your workbench and click on src folder (highlight it).
Click File | Import... OR Right-Click | Import...
Select File System from the Import dialog.
 Select Import Type DialogClick Next.

5. Complete the Import Dialog.
Click browse (for the From Directory value) and navigate to the src (location of your Java files and packages) under the TestProject directory we created in the Simple Project section (you don't need to open, just highlight the folder) then click OK.
 Import Dialog (File System)Select the src folder (or individual folders or files below this level you would like to import).  We are discussing bringing in an entire project, so the entire folder is most appropriate in most of these cases.

Note: the Into Folder location is set to TestProject/src since we highlighted this source folder before initiating the import process.  This coupled with the create folders options will ensure that your package structure is recreated on import.

Click Finish and enjoy!


3. New Project from existing Ant buildfile.



If you were previously coding by hand, there is a good chance you were building using Ant or having exported this project from another IDE that is Ant-based, you may already have a buildfile that we can utilize to bring our project into Eclipse.  

Additional reasons to use Ant buildfiles would be to allow programmers to share project code despite IDE each has chosen to work in; therefore, this approach may be useful in team environment as well.

Example Ant Buildfile:
(build.xml placed in root of project folder at same level as src directory)
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="build" name="TestProject">
    <target name="init">
        <mkdir dir="bin"/>
        <copy includeemptydirs="false" todir="bin">
            <fileset dir="src">
                <exclude name="**/*.launch"/>
                <exclude name="**/*.java"/>
            </fileset>
        </copy>
    </target>
    <target depends="build-subprojects,build-project" name="build"/>
    <target name="build-subprojects"/>
    <target depends="init" name="build-project">
        <javac destdir="bin">
            <src path="src"/>
        </javac>
    </target>
</project>

Open in new window

For more details on Ant and specifically writing a buildfile, please see Ant's manual on the topic here.  Key for this method is the javac task:
<javac destdir="bin">
    <src path="src"/>
</javac>

Open in new window

There must be at least one of these for Eclipse to create a project using this file.

For those that already have their buildfile ready to go, let's get to it...

1. Launch Eclipse.

Note: if you like to have your Java projects in one workspace, then you should copy the entire project folder including the Ant build file to your workspace before going through steps below.

2. Create New Project.
Click File | New | Project, navigate to and choose Java Project from Existing Ant Buildfile.
 Select New Project Type DialogClick Next.

3. Complete New Java Project Dialog.
Click browse (for Ant buildfile) and select the above build.xml file, then click OK.
 New Project Dialog (Ant)If you have multiple javac tasks, then select the declaration to use in defining the project.  For the above, you will see '"javac" task found in target "build-project"', which will be auto-selected since it is the only javac task in our simple buildfile.

Challenge: what is the difference between a buildfile linked to in the file system and one that is not?

Click Finish and enjoy!


4. Import Existing Project: add former projects to new workspace.



Last, but not least, we can utilize the import existing projects approach available in Eclipse.

I use this approach a lot when building new machines or switching between repositories of code or re-installing Eclipse but wanting to pick back up a project previously in an older installation or workspace.

In order for it to work, however, the project has to be an existing Eclipse project or at least we need to make Eclipse believe so.  The key is the .project (hidden file on some operating systems) that is in the root of the project folder.

Example .project File:
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
    <name>TestProject</name>
    <comment></comment>
    <projects></projects>
    <buildSpec>
        <buildCommand>
            <name>org.eclipse.jdt.core.javabuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
    </buildSpec>
    <natures>
        <nature>org.eclipse.jdt.core.javanature</nature>
    </natures>
</projectDescription>

Open in new window


Take the above and save it as .project under TestProject directory.

For different projects, simply change the value of the name tag (i.e., <name>SomeProject</name>) and you can convert other projects to Eclipse projects for this import to work.  Again, this is from Eclipse 3.5.x for Java project, so to ensure you have the correct XML schema I would suggest grabbing the text from a working Eclipse project's .project file and edit as needed.  

Okay, you ready, let's get to it...

1. Launch Eclipse.

2. Import Existing Project.
Click File | Import... OR Right-Click (in Project or Package Explorer Pane) | Import...
Select Existing Projects into Workspace from the Import dialog.
 Select Import Type DialogClick Next.

3. Complete the Import Dialog.
Click browse (for select root directory entry) and choose our TestProject directory, then click OK.
 Import Dialog (Existing)If you had multiple project files (e.g., if you picked a workspace directory), then you can import multiple projects at one time.

Note: if you like to have your Java projects in one workspace, then you should ensure to check "copy projects into workspace" option.

Click Finish and enjoy!

I find this very useful as I said for restore of environment after clean installation, as can store my old workspace as an archive on server and then re-import all my projects at one time.


Results

You should now have a project in your Eclipse workbench that looks like the following :
 Workbench with TestProject
Depicted in the illustration :
 
Project named "TestProject"
Source folder "src"
Package name "testpackage"  
Class file "TestClass.java"  

Challenge: explore some of the common tasks you may need to do to further make your project ready for development in Eclipse like configuring additional libraries, source folders or references in your build path.


Summary

As you can see these methods are very useful, but at the same time straight-forward; therefore, you should be able to get your project up and running with low effort in order to get to what you love: Java!

Happy coding!

Best regards,


Kevin (aka MWVisa1)


Acknowledgements:

Special thanks to EE Member and Page Editor mark_wills for his guidance on the structure of this Article!  

Thanks to him and to you all for taking the time to read this.

If you liked this Article, it helped you solve an issue you were having getting your project started in Eclipse or just because...please remember to vote Yes below to show your support.


Related Resources / References:

Eclipse.org Web-site
http://www.eclipse.org/
http://www.eclipse.org/downloads/

Java Development Kit (JDK) Download
http://java.sun.com/javase/downloads/

The Java Tutorials
http://java.sun.com/docs/books/tutorial/

Writing a Simple (Ant) Buildfile
http://ant.apache.org/manual/using.html
(or use manual landing page to get table of contents)
http://ant.apache.org/manual/

   
6
Comment
Author:Kevin Cross
3 Comments
 
LVL 32

Expert Comment

by:awking00
Kevin,
Just want to say thanks. I was wondering how I could import a project that was created in JDeveloper using an Ant buildfile, since there was no existing .classpath or .project file and the standard import wizard wouldn't work. The New Project from existing Ant buildfile procedure you provided was exactly what I needed and very easy to follow. Thanks again.
0
 
LVL 60

Author Comment

by:Kevin Cross
Glad to hear it.  Thank you for reading and, further, taking the time to post nice words and vote!
It makes it worthwhile to know something I have written was helpful to others.

Kevin
0
 
LVL 7

Expert Comment

by:gudii9
why we need to select ' java project from ant build option'. I usually import directly 'import from

file-->import-->existing project into workspace--> select one of two radio buttons like 'select root directory' or 'select archive file'
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Join & Write a Comment

Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month