3

I put some .txt files under the src folder (in the resources folder).

But I can't create a valid File at runtime from this resource.

String path = this.getClass().getResource("/resources/file.txt").getFile();
File file = new File(path);

if (!file.exists()) {
}

I run my program from eclipse. I didn't put in classpath anything.
I want my text files to be embedded into the .jar file, when I run my app I want to grab those files and copy them into some location.

UPDATE

if I do InputStream is = getClass().getResourceAsStream("/resources/file.txt");

I get the stream!!

Robin Green
  • 32,079
  • 16
  • 104
  • 187
kenny
  • 2,000
  • 6
  • 28
  • 38
  • How do you try to create a file? Do you use `new File(path);`, when yes is there any exception? – CSchulz Nov 16 '11 at 13:29
  • Did you tried giving absolute path? – nebula Nov 16 '11 at 13:29
  • Are you sure that the txt file is making it to the classes directory? Depending on how you're building this might not be the case. – Andres Olarte Nov 16 '11 at 13:31
  • @Andres Olarte, what do you mean how I build it? I put the file to this folder and run from eclipse. – kenny Nov 16 '11 at 13:33
  • @CSchulz , no exception !file.exists() is triggerd – kenny Nov 16 '11 at 13:33
  • How do you try to create a new file? – CSchulz Nov 16 '11 at 13:35
  • Did you read the [API doc](http://download.oracle.com/javase/6/docs/api/java/lang/Class.html#getResource(java.lang.String)) ??? – PeterMmm Nov 16 '11 at 13:38
  • @CSchulz, I don't think I understand you. I put the file into the folder (drag and drop using eclipse). then I want to load like the code above – kenny Nov 16 '11 at 13:40
  • Yeah I thought there is a missunderstanding caused of your **create**. Perhaps you shall add that you tried to open the file without success or write *I can't create a file instance*. – CSchulz Nov 16 '11 at 13:42
  • I want to my text file to be embedded resource that i drop into my project (so it will delivered inside the jar) and on runtime i want to extract it – kenny Nov 16 '11 at 13:48
  • is `resources` in the classpath? also try doing `getClass().getClassLoader().getResource(...)` instead. – aishwarya Nov 16 '11 at 13:54
  • @aishwarya, getClassLoader() didn't help. – kenny Nov 16 '11 at 14:00
  • are you sure /resources/file.txt is in the classpath? – aishwarya Nov 16 '11 at 14:03
  • @aishwarya, no... I just put where my "src" is – kenny Nov 16 '11 at 14:05
  • :) so the resources directory needs to be copied over to where your classes are going (and eventually into the jar that you intend to build). unless the file's in the classpath, getResource() won't find it :) try and let me know if it works – aishwarya Nov 16 '11 at 14:11
  • @aishwarya, the classes are copied automatically into bin folder the resource folder also there!! – kenny Nov 16 '11 at 14:12
  • @all, please see my last update – kenny Nov 16 '11 at 14:24

2 Answers2

2

As you already discovered yourself soon after posting your question, this works:

InputStream is = getClass().getResourceAsStream("/resources/file.txt");

The reason this works, while your original code doesn't, is because a "file" inside in a zip file (a jar file is a zip file) is not a real file until it has been extracted. But extracting the file is what you are trying to do, so at that point in your program, it's not a real file. So this question is an X-Y problem: you wanted to create a File object, but that wasn't possible - you needed to refer back to what you were originally trying to do, which was read from the zip entry.

Robin Green
  • 32,079
  • 16
  • 104
  • 187
0

You said that you are using eclipse, and that you dragged and dropped your text files into the "src" package. "src" is not a package. It is simply a file system directory. By default in a Java project in eclipse all your source code is stored in a directory called "src" and all your .class files are stored in a directory called "bin". getClass().getResource() resolves to the location of your .class files. You must move the text files into the "bin" directory.

What package is your class in?

I wrote very similar code to yours in the default package and ran it in eclipse.

import java.io.File;

public class ResourceTest {
    public static void main(String[] args) {
        ResourceTest rt = new ResourceTest();
        rt.openFile();
    }

    public void openFile() {
        String path = this.getClass().getResource("/resources/file.txt").getFile();
        File file = new File(path);

        System.out.println(path);
        System.out.println(file.getAbsolutePath());
        System.out.println(file.exists());
    }
}

I see this output:

/C:/Users/rab29/Documents/eclipse/Overflow/bin/resources/file.txt
C:\Users\rab29\Documents\eclipse\Overflow\bin\resources\file.txt
true
Barzee
  • 897
  • 3
  • 15
  • 30
  • when i build the project it auto copies folder with text files into bin – kenny Nov 16 '11 at 13:54
  • bin is where it should be located. What happens when you leave the /resources folder in bin and run your program? Are you running your program from eclipse or are you packaging it as jar file and running it from the jar file? – Barzee Nov 16 '11 at 14:43
  • (Dragging an external file/creating a folder) into an Eclipse project tree under "src" folder where the java source codes are located will automatically (copy the file into/create the folder to) "bin" tree where the classes are generated (1:1 relationship). So, "/resources/file.txt" folder under "src" = "/resources/file.txt" folder under "bin" – ecle Nov 16 '11 at 14:44
  • The resulting .class must refer correctly from which package tree structure the "/resources/file.txt" exists. It looks like this .class must be under " package" node to be able to access the resource at "/resources/file.txt". Imagine this like browsing via `dir` or `ls` command prompt. You must now where you are relatively to that file... – ecle Nov 16 '11 at 14:52
  • Can you tell the name of the .java file containing the code above and its package name if it exists. – ecle Nov 16 '11 at 15:11
  • Is your class in a package or is it in the default package? – Barzee Nov 16 '11 at 17:33