2

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

2 Answers 2

2

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.

0

The following should work

IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
IJavaProject javaProject = JavaCore.create(project);
1
  • Hey beny23....i used that code...but when i use javaProject.open(monitor); where monitor is of type IProgressMonitor its throwing an exception...
    – Steven
    Jan 29, 2010 at 10:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.