Artifact Building Issues

Hey everyone!

I've just finished a project. In it there is the default src package (marked as source root), and two directories that I added, one contains textures and the other text files. I've marked both as resources roots.
I'm loading the pictures and text files in the program, and when running from the IDE it all works fine.
I've made an artifact using project structure > artifacts > new artifact (from module with dependencies), chose the main class and accepted, and then used build > build artifact > build.
It created the jar file and everything was ok, although when running it with the JAR file, all the textures were missing, and it only showed the drawn text / rects etc.
In the code, I loaded the textures with "resources/filename.png", because it's in the resources folder.
Looking in the jar file, I can see that all the textures and text files that used to be in the two directories, are in the base jar instead, and the only directories are src and META-INF, so I think that's the problem.
How can it be fixed?

Thanks!

P.S. It's my first post in the forums, so sorry if I'm doing something wrong.
P.P.S. Sorry for my bad English, I'm a foreign speaker.

7 comments
Comment actions Permalink

Hello.

Please correct me if I've got it wrong: your resource directory structure is:
. resource/a-texture.png
You have marked "resource" as a resource root. Then on building a jar you get:
. artifact.jar/a-texture.png
But you want:
. artifact.jar/resource/a-texture.png

If I'm correct, then this is by design. A suggestion can be to create one more directory in the project, like:
. resource/resource/a-texture.png

Then the first "resource" will be a resource root (lost in the artifact), and the deeper "resource" will stay in the jar.

Regards,
Alexander.

0
Comment actions Permalink

But then I won't be able to run it from the IDE..

0
Comment actions Permalink

It should be equally possible: the classpath of an application run by IDEA is the one that is packed into jar.

Can you attach a sample of the project? I could look and see, is it possible to do this or that.

Regards,
Alexander.

0
Comment actions Permalink

How do you load images? E.g. via:

... image = ImageIO.read(... what is here? ...);

Or somehow else?

Thanks,
Alexander.

0
Comment actions Permalink

I use for example: (in a for loop)
tileset_air[i] = createImage(new FilteredImageSource(image.getSource(), new CropImageFilter(0, i * 26, 26, 26)));

And in the non-tiled ones:
tileset_res[0] = new ImageIcon("resource/cell.png").getImage();

0
Comment actions Permalink

I think this way you are reading the image not from the classpath. In IDEA your application's working directory is the project root:
.../Tower Defence Game/
And by URL "resource/cell.png" you take the image from the project sources:
.../Tower Defence Game/resource/cell.png
But this will not work in production anyway, since in production a person running the app has no such source tree.

Please try the following in one of your statements:
... someVar = new ImageIcon(ClassLoader.getSystemResource("cell.png")).getImage();

If this loads the image successfully, then you can change the parameter to:
... someVar = new ImageIcon(ClassLoader.getSystemResource("resource/cell.png")).getImage();

And also change your resource directory structure to resource/resource/*.png

Regards,
Alexander.

0

Please sign in to leave a comment.