views:

64

answers:

2

Hi,

I am developing a plugin.In this i take project as input from textbox which is a string , but it has to be converted to IJavaProject type before proceeding. How can i do that ?

Thanks

A: 

The following should work

IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
IJavaProject javaProject = JavaCore.create(project);
beny23
@beny23 - thanks very much
Steven
Hey beny23....i used that code...but when i use javaProject.open(monitor); where monitor is of type IProgressMonitorits throwing an exception...
Steven
+2  A: 

If projectName does not exist, IProject, which is just a handle, will be null. IJavaProject will also be null... so I would not recommend beny23's solution.

The javadoc for JavaCore.create(IProject) states, "no check is done at this time on the existence or the java nature of this project".

See this thread for creating a Java Project programmatically from scratch. Extract

final IJavaProject javaProject = JavaCore.create(project);
final IProjectDescription projectDescription =
  workspace.newProjectDescription(projectName);
projectDescription.setLocation(null);
project.create(projectDescription, new SubProgressMonitor(progressMonitor, 1));

You can check if is has actually been created with:

IJavaProject.getUnderlyingResource().exists();

See also this thread as another code example of Java Project creation.

That thread also creates project, even though their nature is more complete than just Java.

VonC