Skip to Content
Author's profile photo Robinson Selvamani

Copying file from plugin project into the local system

Imagine a situation where you want to copy a file from a simple eclipse plugin project into local system as soon as the eclipse workbench  opens or when the software containing the said plugin is being installed. One way to achieve this task is to use org.eclipse.ui.startup extension. This blog explains how to use this extension and to copy file from the plugin project into local system.

Eclipse plugins are loaded whenever the user needs their functionalities. This is called lazy plug-in activation. The extension org.eclipse.ui.startup is designed to break this behavior. It forces the workbench to activate the plug-in as soon as the workbench window opens. And this extension should be used only in exceptional situations as it can  affect start-up performance.

Note that wherever I used the term code it meant Java code.

Do the following steps to copy a file attached with a plugin project into the local system.

  1. Create a plugin project, say demo.startup in eclipse IDE.
  2. Make sure that you have org.eclipse.ui as dependency.
  3. Add the extension, org.eclipse.ui.IStartup.
  4. Remove the entry Bundle-ActivationPolicy: lazy from MANIFEST.MF.
  5. Attach the file, say settings.xml, which you want to copy to the project.
  6. Create a package, say demo.startup.action under src folder.
  7. Create a java class, say FileCopier.

The FileCopier class should implement the interface IStartup (org.eclipse.ui.IStartup) and should override the method, earlyStartup() as given below.

import org.eclipse.ui.IStartup;

public class FileCopier implements IStartup {


     public void earlyStartup() {           

          <Java code to copy file into local system during start up>



Note that whenever the workbench opens, the plugin is loaded immediately and  earlyStartup() method is called automatically.

Now open the plugin.xml file and insert your startup class name as given below.




         <startup class=“demo.startup.action.FileCopier”/>



Copying the file

Read the source  file (i.e. settings.xml) from the project

InputStream  inputStream =  this.getClass().getClassLoader().getResourceAsStream (“settings.xml”);

Create a local file (i.e. target file) where you want to store the content of settings.xml.

File targetFile = new File(“C:/temp/settings.xml”);

Before copying make sure that the target file already exists. If exists check if the contents are same. If same, do not copy. Otherwise either overwrite or make a copy of existing file and then do copying. I prefer the second approach.


            InputStream targetInputStream = new FileInputStream(targetFile);







            inputStream = getClass().getResourceAsStream(“settings.xml”);


            FileOutputStream outputStream;

            outputStream = new FileOutputStream(targetFile);

            byte[] buf = new byte[1024];

            int i = 0;

            while((!=-1) {

                        outputStream.write(buf, 0, i);




Here is the code to check if the contents of the files are equal.

private boolean contentEquals(InputStream inputStream, InputStream targetInputStream) throws IOException {

                        if (!(inputStream instanceof BufferedInputStream)) {

                                    inputStream = new BufferedInputStream(inputStream);


                        if (!(targetInputStream instanceof BufferedInputStream)) {

                                    targetInputStream = new BufferedInputStream(targetInputStream);


                        int ch =;

                        while (-1 != ch) {

                                    int ch2 =;

                                    if (ch != ch2) {

                                                return false;


                                    ch =;


                        int ch2 =;

                        return (ch2 == -1);


Here is the code to rename the existing file.

private void renameFile(File targetFile) throws IOException {

      for(int i=1;;i++){

          String newName = targetFile.getName().substring(0, targetFile.getName().lastIndexOf(“.”));

          newName = newName.concat(” – Copy (“+i+”)”+”.xml”);

          File file = new File(targetFile.getParent()+File.separator+newName);




        FileInputStream inputStream = new FileInputStream(targetFile);

        FileOutputStream outputStream = new FileOutputStream(file);

        byte[] buf = new byte[1024];

        i = 0;

        while((!=-1) {

            outputStream.write(buf, 0, i);





Note: Catch the exceptions wherever required.

Restart the eclipse IDE. The file must be copied into the specified location in your local system. In case the file already exists and the content is not the same as that of the new file then it will create a copy like settings – Copy.xml. If settings – copy.xml file also exists then it will create settings – Copy (2).xml and so on.

Assigned Tags

      1 Comment
      You must be Logged on to comment or reply to a post.
      Author's profile photo Abhinaw Sachan
      Abhinaw Sachan

      Super cool article..... 🙂