I spent more than two days in investigating this but my bad it didn't worked out.
My problem
I am writing one program (osgi bundle) to get IJavaProject object to get ICompilationUnits of java files to see proposals made by eclipse.
Where I stuck
My IJavaProject reference is always coming null. I tried various combinations.
// Sample is a Java Project I created separately with one Test file, I also accessed this project in which I am coding but its a plugin
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject("Sample");
System.out.println(project.getLocation()); // prints absolute path
IProjectNatureDescriptor[] nids = project.getWorkspace().getNatureDescriptors();
for (int i = 0; i < nids.length; i++) {
System.out.println(nids[i].getNatureId()); // prints all natures associated to IProject
}
System.out.println(project.getWorkspace().validateNatureSet(project.getDescription().getNatureIds()).isOK()); // print true
if (project.isOpen()){
System.out.println(project.hasNature(JavaCore.NATURE_ID)); // prints true
}
final IJavaProject javaProject = JavaCore.create(project);
System.out.println("jp=null ?? ====="+(javaProject==null)); // this prints false , I am amazed
System.out.println("jp=null ?? ====="+(javaProject==null)); // throws Null pointer exception
........
Also, Before this in my project there was linkage error as I was supplying jars from my lib and eclipse was also providing. By excluding couple of jars from my lib made that working.
Can anyone help me why is it coming Null pointer always. What am I doing wrong here?
Exception trace
java.lang.NullPointerException
at org.eclipse.jdt.internal.core.JavaModelManager.getInfo(JavaModelManager.java:1577)
at org.eclipse.jdt.internal.core.Openable.isOpen(Openable.java:376)
at org.MYPACKAGE.manager.RoasterTransformManager.execute(RoasterTransformManager.java:73)
at org.MYPACKAGE.manager.TransformManager.transform(TransformManager.java:39)
at org.MYPACKAGE.Transform.performTransform(Transform.java:46)
at org.MYPACKAGE.Transform.begin(Transform.java:56)
at org.MYPACKAGE.Activator.start(Activator.java:22)
at org.eclipse.osgi.internal.framework.BundleContextImpl$3.run(BundleContextImpl.java:771)
at org.eclipse.osgi.internal.framework.BundleContextImpl$3.run(BundleContextImpl.java:1)
at java.security.AccessController.doPrivileged(Native Method)
at org.eclipse.osgi.internal.framework.BundleContextImpl.startActivator(BundleContextImpl.java:764)
at org.eclipse.osgi.internal.framework.BundleContextImpl.start(BundleContextImpl.java:721)
at org.eclipse.osgi.internal.framework.EquinoxBundle.startWorker0(EquinoxBundle.java:941)
at org.eclipse.osgi.internal.framework.EquinoxBundle$EquinoxModule.startWorker(EquinoxBundle.java:318)
at org.eclipse.osgi.container.Module.doStart(Module.java:571)
at org.eclipse.osgi.container.Module.start(Module.java:439)
at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.incStartLevel(ModuleContainer.java:1582)
at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.incStartLevel(ModuleContainer.java:1562)
at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.doContainerStartLevel(ModuleContainer.java:1533)
at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.dispatchEvent(ModuleContainer.java:1476)
at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.dispatchEvent(ModuleContainer.java:1)
at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230)
at org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.run(EventManager.java:340)
Stackoverflow is luck for me. Most of the times when I am struggling with issue since long and posts my question it resolves quickly.
I was looking in bugs forum of eclipse and I find this https://bugs.eclipse.org/bugs/show_bug.cgi?id=75969.
According to this if there are certain jars in your lib folder of project it won't work. You need to define those as list of dependent plugins and get them from eclipse\plugins instead of lib. This happens in my case.
Still if any one of you know how can I make headless environment without involving eclipse i.e. using command line please let me know. For now I am getting object of IJavaProject.
Thanks