简体   繁体   中英

Importing File/Directory into IJavaProject - Eclipse JDT

So I've been able to successfully generate an IJavaProject from within my Eclipse Plug-In, and now I would like to add packages to this IJavaProject. These packages are located on the file system, and are to be selected by the user. I have created two folders, "bin" and "src".

I want to be able to add files (specifically, a directory that contains packages) to my project just as you would if you dragged-and-dropped files into a project in eclipse.

How would I go about programmatically adapting these files into a Java Model format so I can add them into my IJavaProject? If I could even just only read a .java file into the model, that would be great.

I was thinking maybe it has something to do with IClasspath, but I really haven't been able to find any information on it. I may be overlooking it, however.

Thanks in advance.

With a little searching through the API, I found a solution.

You can create a package from the project by calling:

(Declared somewhere...)

IFolder srcFolder = myJavaProject.getProject().getFolder("src");

Assuming you have already created a suitable folder, "src", within your project already.

IPackageFragment frag = myJavaProject.getPackageFragmentRoot(srcFolder)
    .createPackageFragment("mypackage",false, null);
IFile newFile = ((IFolder)frag.getResource)).getFile(name);
FileInputStream fis = new FileInputStream(new File("myFile.java");
newFile.create(fis, false, null);

You don't have to declare the files as ICompilationUnits or anything like that because if you add a java source file to an element that is already declared as a package, then it automatically recognizes that source file as part of the Java Model, aka an instance of ICompilationUnit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM