0

Can anyone tell me how I can make a file recognised by my plug in project. I have one pom. xml file in my project path like "AA/pom.xml" and I was able to copy this file and make a new one in another location. But when I'm tying to do the same thing in my plug in project, I'm getting FileNotFoundException.

Below is the code which works in a simple java project but not in eclipse plug in project.

private void createPomFile(String location, String projectName, String string) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("\\pom.xml"), "UTF-8"));
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(location + projectName + string), "UTF-8"));

    String line = null;

    while ((line = reader.readLine()) != null) {
        writer.write(line);
        writer.write("\n");
    }
    // Close to unlock.
    reader.close();
    // Close to unlock and flush to disk.
    writer.close();

}

StackTrace:

java.io.FileNotFoundException: \pom.xml (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at createservicestemplate.wizards.SampleNewWizard.createPomFile(SampleNewWizard.java:320)
at createservicestemplate.wizards.SampleNewWizard.doFinish(SampleNewWizard.java:288)
at createservicestemplate.wizards.SampleNewWizard.access$0(SampleNewWizard.java:118)
at createservicestemplate.wizards.SampleNewWizard$1.run(SampleNewWizard.java:86)
at org.eclipse.jface.operation.ModalContext$ModalContextThread.run(ModalContext.java:121)
Pablo Lozano
  • 10,122
  • 2
  • 38
  • 59
  • How are you running this code? Within eclipse or via the command line? – JamesB Jul 07 '14 at 09:20
  • Where is you "pom.xml" located? I dont think it is under the root directory? – Jens Jul 07 '14 at 09:22
  • I don't understand what is your exact problem. The code above just copies the pom file, so what? The above code throws the `FileNotFoundException`? Then it is obvious that compiler cannot find the `pom` file. – Eypros Jul 07 '14 at 09:23
  • I'm running it within eclipse (run as Eclipse application). – user3607601 Jul 07 '14 at 09:37
  • pom is located under the project directory. If "A" is my plugin project then pom resides in "A/pom.xml". – user3607601 Jul 07 '14 at 09:37
  • I think eclipse plugin project runs in a different path that's why it cannot recognize my pom file location. – user3607601 Jul 07 '14 at 09:39
  • Please post the stacktrace of the exception.. What is the value of location, projectName and string. Also please follow Java naming conventions to make the code self understandable. – AJJ Jul 07 '14 at 10:12
  • i guess, your java file will be located inside the src folder inside some package (sub folders) and pom.xml must be located outside the src floder. So when running the java file, the code looks for pom.xml in the src folder. – AJJ Jul 07 '14 at 10:20
  • my pom.xml file resides under the project directly like "Project1\pom.xml" – user3607601 Jul 07 '14 at 10:24

2 Answers2

0

You will have to remove the \\ in the argument of FileInputStream. So that the Java code searches for the the file directly under your project folder.

In your code, the java class searches for pom.xml right inside the directory where your workspace is placed.

For Example, if you have the workspace in "D:" drive, then upon executing your code with \\pom.xml in FileInputStream, then your code searches for presence of D:\\pom.xml. Hence pom.xml is present in your project as AA\\pom.xml, the exception is thrown.

BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("pom.xml"), "UTF-8"));

UPDATE after the user need: For Eclipse Plugin

For eclipse plugin the below will work. Figured it out after your latest comment. I have checked the same in Eclipse plugin and it is working for me.

       try {
            String line = null;
            FileInputStream fi = (FileInputStream) your_class_name.class.getResourceAsStream("/pom.xml");
            BufferedReader bf = new BufferedReader(new InputStreamReader(fi));
            while((line = bf.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
AJJ
  • 3,570
  • 7
  • 43
  • 76
  • It didn't work either.The code I pasted perfectly works from a simple java project with BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("pom.xml"), "UTF-8")); But it doesn't work for eclipse plug in project. – user3607601 Jul 07 '14 at 10:47
  • Didn't work. It's taking D:\eclipse 4.2\pom.xml as the new path. :( – user3607601 Jul 07 '14 at 11:51
  • @user3607601 I got this working. Please check my updated answer where you should read the file inside the project as a stream. – AJJ Jul 08 '14 at 05:40
  • What is BreakPointInfoView.class? – user3607601 Jul 08 '14 at 10:56
  • That is my class where i tried it. You can put your class there. Or use as getclass.getResourceAsStream – AJJ Jul 08 '14 at 10:58
0

This line

BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("\\pom.xml"), "UTF-8"));

says: "Read a file called pom.xml in the root directory of the current disk drive" (that is what \ means).

Check what is your current directory and where is that pom file:

System.out.println(" Working directory is "+ new File("temp.txt").getCanonicalPath());
Pablo Lozano
  • 10,122
  • 2
  • 38
  • 59