Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jdt-core-dev] How to open existing java package (source code) programatically?

Hi,

I'm trying to open some existing java sources with eclipse. The purpose being to use ASTParser (with binding resolution) to do do some static analysis. But I'm still struggling to get them opened as ICompilationUnits. The source files exist on local disk. I get the exception:

Java Model Exception: Java Model Status [D:\code\java does not exist]
at org.eclipse.jdt.internal.core.JavaElement.newJavaModelException(JavaElement.java:501)
at org.eclipse.jdt.internal.core.Openable.generateInfos(Openable.java:245)
at org.eclipse.jdt.internal.core.JavaElement.openWhenClosed(JavaElement.java:514)
at org.eclipse.jdt.internal.core.JavaElement.getElementInfo(JavaElement.java:251)
at org.eclipse.jdt.internal.core.Openable.open(Openable.java:431)
at com.blogspot.highbit.codester.Application.start(Application.java:86)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:193)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:386)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:549)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:504)
at org.eclipse.equinox.launcher.Main.run(Main.java:1236)
at org.eclipse.equinox.launcher.Main.main(Main.java:1212)



The code I'm using is:

public Object start(IApplicationContext context) throws Exception {
IProject proj = null;
IJavaProject javaProj = null;
try {
IWorkspace ws = ResourcesPlugin.getWorkspace();
IWorkspaceRoot wsRoot = ws.getRoot();
proj = createTmpProject(wsRoot);
System.out.format("cwd:%s proj:%s\n", System.getProperty("user.dir"), proj.getName());
System.out.flush();
assert proj.exists();
IProjectDescription description = proj.getDescription();
String[] curNatureStrings = description.getNatureIds();
String[] newNaturesStrings = new String[curNatureStrings.length + 1];
System.arraycopy(curNatureStrings, 0, newNaturesStrings, 0, curNatureStrings.length);
newNaturesStrings[curNatureStrings.length] = JavaCore.NATURE_ID; // add the Java Nature
description.setNatureIds(newNaturesStrings);
Map<String, Object> args = context.getArguments();
String[] cmdArgs = (String[])args.get(IApplicationContext.APPLICATION_ARGS);
Path dir = new Path(cmdArgs[0]);
description.setLocation(dir);  // create the project in the source directory. didn't work without this step too
proj.setDescription(description, null);
javaProj = JavaCore.create(proj);
javaProj.open(null);
assert javaProj.exists();
System.out.format("Args: dir:%s class:%s\n", cmdArgs[0], cmdArgs[1]);
IClasspathEntry cp = JavaCore.newSourceEntry(dir);
javaProj.setRawClasspath(new IClasspathEntry[] { cp }, null);  // doesn't work without this step too. complains dir is not in build path
IPackageFragmentRoot srcRoot = javaProj.getPackageFragmentRoot(cmdArgs[0]);
IPackageFragment[] pkgs = javaProj.getPackageFragments();
srcRoot.open(null);

I get the exception in the last line for the above code segment. Any idea how to get this working?

I'm using eclipse 3.4.2 (Build id: M20090211-1700).



Thanks & Regards,
Tharaka


Back to the top