Create a Java project by programming in eclipse

Source: Internet
Author: User
Tags addall

Steps:
1. Create an org. Eclipse. Core. Resources. iproject
2. Set description for the project, that is, generate the. project file.
Note: A. javacore. nature_id = "org. Eclipse. jdt. Core. javanature" indicates a Java project.
B. When you place description in a project, ensure that the project exists and is being opened.
3. Convert org. Eclipse. Core. Resources. iproject to a Java project.
Ijavaproject javaproject = javacore. Create (project );
4. Obtain the JRE path of eclipse and the path of the current project, and set the directory for storing source code and *. class files.
This part generates the content in the. classpath file.
5. Compile a string into a Java file and the corresponding. Class file.

Example:
Package org. wliu. sample. createnewjavaproject. actions;

Import java. util. arrays;
Import java. util. hashset;
Import java. util. Set;

Import org. Eclipse. Core. Resources. ifolder;
Import org. Eclipse. Core. Resources. iprojectdescription;
Import org. Eclipse. Core. Resources. iresource;
Import org. Eclipse. Core. Resources. iworkspaceroot;
Import org. Eclipse. Core. Resources. resourcesplugin;
Import org. Eclipse. Core. runtime. coreexception;
Import org. Eclipse. Core. runtime. nullprogressmonitor;
Import org. Eclipse. Core. runtime. path;
Import org. Eclipse. Core. runtime. subprogressmonitor;
Import org. Eclipse. jdt. Core. iclasspathentry;
Import org. Eclipse. jdt. Core. ijavaproject;
Import org. Eclipse. jdt. Core. ipackagefragment;
Import org. Eclipse. jdt. Core. ipackagefragmentroot;
Import org. Eclipse. jdt. Core. javacore;
Import org. Eclipse. jdt. Core. javamodelexception;
Import org. Eclipse. jdt. UI. preferenceconstants;
Import org. Eclipse. jface. Action. iaction;
Import org. Eclipse. jface. dialogs. messagedialog;
Import org. Eclipse. jface. Viewers. iselection;
Import org. Eclipse. UI. iworkbenchwindow;
Import org. Eclipse. UI. iworkbenchwindowactiondelegate;

/**
* Our sample action implements workbench action delegate.
* The Action proxy will be created by the workbench and
* Shown in the UI. When the user tries to use the action,
* This delegate will be created and execution will be
* Delegated to it.
* @ See iworkbenchwindowactiondelegate
*/
Public class sampleaction implements iworkbenchwindowactiondelegate {
Private iworkbenchwindow window;
/**
* The constructor.
*/
Public sampleaction (){
}

/**
* The action has been activated. The argument of
* Method represents the 'real' action sitting
* In the workbench UI.
* @ See iworkbenchwindowactiondelegate # Run
*/
Public void run (iaction action ){
Messagedialog. openinformation (
Window. getshell (),
"Createnewjavaproject plug-in ",
"Hello, eclipse world ");
// Create the resource project
Iworkspaceroot root = resourcesplugin. getworkspace (). getroot ();
Org. Eclipse. Core. Resources. iproject project = root. getproject ("wliu ");
Ijavaproject javaproject = NULL;
If (! Project. exists ()){
Iprojectdescription prjdesc = root. getworkspace (). newprojectdescription (project. getname ());
String [] oldnatureids = prjdesc. getnatureids ();
String [] newnatureids = new string [oldnatureids. Length + 1];
System. arraycopy (oldnatureids, 0, newnatureids, 0, oldnatureids. Length );
Newnatureids [oldnatureids. Length] = javacore. nature_id; // define this is a Java Project
Prjdesc. setnatureids (newnatureids );
Try {
Project. Create (New nullprogressmonitor ());
Project. Open (iresource. background_refresh, new subprogressmonitor (New nullprogressmonitor (), 1000 ));
Project. setdescription (prjdesc, new nullprogressmonitor ());
} Catch (coreexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}

} Else {
Try {
Iprojectdescription prjdesc = project. getdescription ();
String [] oldnatureids = prjdesc. getnatureids ();
String [] newnatureids = new string [oldnatureids. Length + 1];
System. arraycopy (oldnatureids, 0, newnatureids, 0, oldnatureids. Length );
Newnatureids [oldnatureids. Length] = javacore. nature_id; // define this is a Java Project
Prjdesc. setnatureids (newnatureids );
Project. Open (iresource. background_refresh, new subprogressmonitor (New nullprogressmonitor (), 1000 ));
Project. setdescription (prjdesc, new nullprogressmonitor ());
} Catch (coreexception e ){
E. printstacktrace ();

}
}
Javaproject = javacore. Create (project );
// Create the classpath entries

// Get the platform's JRE classpath
Iclasspathentry [] jreclasspaths = preferenceconstants. getdefaultjrelibrary ();
// Get the project existing classpath
Iclasspathentry [] oldclasspathentries = NULL;
Try {
Oldclasspathentries = javaproject. getrawclasspath ();
} Catch (javamodelexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Set <iclasspathentry> newclasspathentries = new hashset <iclasspathentry> ();
Newclasspathentries. addall (arrays. aslist (jreclasspaths ));
Newclasspathentries. addall (arrays. aslist (oldclasspathentries ));
// Try {
//
Javaproject. setrawclasspath (newclasspathentries. toarray (New
Iclasspathentry [newclasspathentries. Size ()]), new
Nullprogressmonitor ());
//} Catch (javamodelexception e ){
/// Todo auto-generated Catch Block
// E. printstacktrace ();
//}

// Create the output location
Ifolder binfolder = javaproject. getproject (). getfolder ("bin ");
Try {
If (! Binfolder. exists ()){
Binfolder. Create (true, true, new nullprogressmonitor ());
}
} Catch (coreexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Try {
Javaproject. setoutputlocation (binfolder. getfullpath (), new nullprogressmonitor ());
} Catch (javamodelexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}

// Create the source folder
Ifolder srcfolder = javaproject. getproject (). getfolder ("src ");
If (! Srcfolder. exists ()){
Try {
Srcfolder. Create (true, true, null );
} Catch (coreexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}

// Add the SRC classpath entry to the. classpath File
Iclasspathentry classpathentry = javacore. newsourceentry (srcfolder. getfullpath ());
Newclasspathentries. Add (classpathentry );
Iclasspathentry removeentry = javacore. newsourceentry (New Path ("/" + project. getname ()));
If (newclasspathentries. Contains (removeentry )){
Newclasspathentries. Remove (removeentry );
}
Try {
Javaproject. setrawclasspath (newclasspathentries. toarray (New
Iclasspathentry [newclasspathentries. Size ()]), new
Nullprogressmonitor ());
} Catch (javamodelexception e ){
// Todo auto-generated Catch Block
System. Err. println ("error ");
E. printstacktrace ();
}
System. Out. println ("OK ");

Try {
Ipackagefragmentroot fileroot = javaproject. findpackagefragmentroot (New Path ("/" + project. getname () + "/src "));

Ipackagefragment packagefragment = fileroot. createpackagefragment ("org. talend. sample", true, new nullprogressmonitor ());

String strjava = "package org. talend. sample; public class test {public static void main (string [] ARGs) {" +
"System. Out. println (/" OK /");}}";
Packagefragment. createcompilationunit ("test. Java", strjava, true, new nullprogressmonitor ());

} Catch (javamodelexception e ){

}

}

/**
* Selection in the workbench has been changed. We
* Can change the state of the 'real' action here
* If we want, but this can only happen after
* The delegate has been created.
* @ See iworkbenchwindowactiondelegate # selectionchanged
*/
Public void selectionchanged (iaction action, iselection selection ){
}

/**
* We can use this method to dispose of any system
* Resources we previusly allocated.
* @ See iworkbenchwindowactiondelegate # dispose
*/
Public void dispose (){
}

/**
* We will cache window object in order
* Be Able To provide parent shell for the message dialog.
* @ See iworkbenchwindowactiondelegate # init
*/
Public void Init (iworkbenchwindow window ){
This. Window = window;
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.