Ramy Al Zuhouri
Ramy Al Zuhouri

Reputation: 22006

Include files in a java archive

I mainly followed this thread:
How do you add files to a jar using Netbeans 6.5? I have a class named Card then needs to load a file and return the name of the InputStream.
This is the code to obtain the input:

public String getFilename()
{
    String result= "" + seed + "-" + value + ".png";
    return result;
}
public InputStream getInputStream()
{
    InputStream result;
    result= Card.class.getClassLoader().getResourceAsStream(getFilename());
    return result;
}

The filename is correct, all the files are in the src folder of the project.
If I try to run it with Netbeans 7.2 it works.But if I build the project and move the jar from it's original position, run it, it doesn't work anymore.It doesn't load the files(result is null).
What could the problem be?

Upvotes: 0

Views: 116

Answers (3)

Ramy Al Zuhouri
Ramy Al Zuhouri

Reputation: 22006

The problem was that it didn't find a library, I had to move all the dist folder in order to make it work.

Upvotes: 0

Colton
Colton

Reputation: 645

I would move all the files into their own package "resources".

String result= "/resources/" + seed + "-" + value + ".png";

(This may not fix the problem but it is the recommended solution from various sites when handling resources. I know that this does work for eclipse IDE)

Upvotes: 0

Gilberto Torrezan
Gilberto Torrezan

Reputation: 5277

Try this:

result= Card.class.getResourceAsStream("/"+getFilename());

Upvotes: 1

Related Questions