创建工程:
private IJavaProject project=null;
public void createProject(IProgressMonitor monitor){
try {
BuildPathsBlock.createProject(getProject(),null, new SubProgressMonitor(monitor, 10));
} catch (CoreException e) {
e.printStackTrace();
}
project=JavaCore.create(getProject());
}
public IProject getProject()
{
return ResourcesPlugin.getWorkspace().getRoot().getProject("ProjectName");
}
创建文件夹:
public static void createFolder(IFolder folder, boolean force, boolean local, IProgressMonitor monitor) throws CoreException {
if (!folder.exists()) {
IContainer parent= folder.getParent();
if (parent instanceof IFolder) {
createFolder((IFolder)parent, force, local, null);
}
folder.create(force, local, monitor);
}
}
创建文件:
public void createMetaFile(IProgressMonitor monitor,String folder,String filename){
StringWriter stringwriter = new StringWriter(1000);
stringwriter.write("<config>xxx</config>");
IFile ifile =getProject().getFile(filename);
byte abyte0[] = stringwriter.toString().getBytes();
ByteArrayInputStream bytearrayinputstream = new ByteArrayInputStream(abyte0);
try{
if(!ifile.exists())
ifile.create(bytearrayinputstream, true, null);
else
ifile.setContents(bytearrayinputstream, true, false, null);
}catch(CoreException ex){
ex.printStackTrace();
}
}
创建classpath:
public void createClassPath(IProgressMonitor monitor,String folder){
ClasspathEntry aclasspathentry[] = new ClasspathEntry[2];
//src 作为classpath
IClasspathEntry srcClasspath = JavaCore.newSourceEntry(getProject().getFolder(folder).getFullPath());
IClasspathEntry jrtClasspath = JavaRuntime.getDefaultJREContainerEntry();
aclasspathentry[0] = (ClasspathEntry)srcClasspath;
aclasspathentry[1] = (ClasspathEntry)jrtClasspath;
if(aclasspathentry[1] == null)
{
jrtClasspath = JavaCore.newContainerEntry(new Path("org.eclipse.jdt.launching.JRE_CONTAINER"));
aclasspathentry[1] = (ClasspathEntry)jrtClasspath;
}
try {
String natures[]=new String[] {
"org.eclipse.jdt.core.javanature"
};
//添加 nature
addNatures(natures,getProject(), new SubProgressMonitor(monitor, 10));
createDirectory(monitor,folder);
//设置味工程的classpath
project.setRawClasspath(aclasspathentry, project.getPath().makeAbsolute().append(folder), new SubProgressMonitor(monitor, 5));
} catch (JavaModelException e) {
e.printStackTrace();
} catch(CoreException e1){
e1.printStackTrace();
}
}