简体   繁体   中英

FileNotFoundException in eclipse Plugin project

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)

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();
        }

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());

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