Sunday, July 29, 2007

Programatically Creating a java package in Eclipse

In my last post we saw how we can create a java project programatically in eclipse. In this post we will see how to create a package in this java project.

The steps needed to do this are :

1. Create a source folder under the java project created earlier .

IFolder src = javaProject.getFolder("src");
folder.create(force , local , nullProgressMonitor);
IPackageFragmentRoot packageRoot = javaProject.getPackageFragmentRoot(folder);
IClasspathEntry[] classPath = javaProject.getRawClasspath();
List entries = new ArrayList(Arrays.asList(classpath));
entries.add(JavaCore.newSourceEntry(root.getPath()));

javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[0]) , null ProgressMonitor);

The code becomes pretty clear if you had followed my previous post.


2. Create the actual package .

src.createPackageFragment(packageName, force , nullProgressMonitor);

Yeah thats done , in the next post well see how to create a class programatically