1

I really need your help to solve my own problem. Now, I'm dealing with small code app. In that project folder contain some resource files (*.xlsx, *.png,...). I placed them in current folder with code file. I just wonder that when I run my code in netbean ide, it just worked find.

After I build code project, I get a jar file in "dist" directory. I run it. It open normally since app used JFrame as user interface. However, when I execute some function of that app, it showed me the error log. Here is the error message:

java.io.FileNotFoundException: 
src\sample.xlsx (The system cannot find the path specified)

What's the matter out there?

Here is some pieces of my code:

copyFile(new File("src\\sample.xlsx"),
new File(txtout.getText()+"\\sample.xlsx"));

Node: copyFile function is used for copy file from source to dest.

Here is my project folder structure in Netbean IDE:

  1. Project Name
    • Source Pakage(src)
      • myClass.java, sample.xlsx, etc
timgeb
  • 76,762
  • 20
  • 123
  • 145
sakura
  • 199
  • 1
  • 2
  • 21
  • 1
    Hopefully, this [answer](http://stackoverflow.com/a/9866659/1057230), be able to help somewhat on the topic :-) – nIcE cOw Jun 19 '14 at 04:52

2 Answers2

3

First, never reference src directly, the directory will not exist once the program is built. Second, you can not access resources which have been embedded within in the application context via a File reference, they simply no longer exist on the file system.

Instead, you need to use Class#getResource or Class#getResourceAsStream

URL url = getClass().getResource("/sample.xlsx");
InputStream is = getClass().getResourceAsStream("/sample.xlsx");
// Don't forget to manage your streams appropriately...
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thanks. Let's me try that. – sakura Jun 19 '14 at 04:45
  • Excuse me!! It still stay the same. By the way, As I opened jar file in WinRAR, I saw that sample.xlsx is in there as well. The question is can I reference that file? – sakura Jun 19 '14 at 05:00
  • `sample.xlsx` is no longer a file, it a compressed series of bytes managed by a zip entry. Does the above example return a result or do they return `null` (or an exception)? – MadProgrammer Jun 19 '14 at 05:02
  • As I printed the url out, I saw that it get the absolute file. but it get it does not work. By the way, my folder name has blank space. Is it can be the matter? – sakura Jun 19 '14 at 05:08
  • And the second method, java did not reorganized the `getResourceAs` function. – sakura Jun 19 '14 at 05:10
  • Should be `getResourceAsStream` sorry. If the `getResource` method returned a non-`null` value, it will print out something like `jar:file:/C:/DevWork/personal/java/projects//JavaApplication260/dist/JavaApplication260.jar!/sample.xlsx` - You can not assume where this resource might reside (Netbeans will use the `classes` directory, but when run from the Jar, it will use Jar entry). You CAN NOT reference the resource as a `File`, it simply can not be done with the file embedded within your application. If you want to, move it outside of the `src` and use a relative path instead ;) – MadProgrammer Jun 19 '14 at 05:14
  • 1
    Hi. Just for a clarification related to [this Meta question](http://meta.stackexchange.com/q/234266/187824). Which vote did you cast in [this question](http://stackoverflow.com/q/24299064/1369235)? – Himanshu Jun 19 '14 at 05:42
  • 1
    @hims056 Wow, okay, I think I went for "why does this code not work" option – MadProgrammer Jun 19 '14 at 05:44
2

Well you can create a folder named resources under the src folder put your resources in it and use them in your code by using getResourceAsStream() and getResource() methods that can access the embedded resources.Clean and Build will compile the code and embed the contents of the resources folder into the application’s .jar file. Ways of Accessing resources :

    String pathToImage = "resources/images/filling.png";
    InputStream stream= ClassName.class.getResourceAsStream(pathToImage );

   String pathToImage = "resources/images/filling.png";
   InputStream stream= ClassName.class.getResource(pathToImage );

please refer the link information

Community
  • 1
  • 1
SparkOn
  • 8,806
  • 4
  • 29
  • 34