Exporting Jar with Images - java

I am trying to export my Runnable Jar file that includes the image resources that goes along with my program. When I run the program in eclipse the paths work perfectly but when trying to export the program there are no images. I have been exporting by choosing the package all contents option.

Since your image files are stored in jar as resources you should retrieve them as resources. Hers is how

Related

How to include resources in a Java game on MacOS?

I have written a game with external resource files (images, sounds, etc..) in Java, which works on Windows when I export the .jar to .exe but not on MacOS when I export the .jar to .app .
I wrote the game in the Eclipse IDE and exported it as a runnable .jar file from there too.
My process for making it run on Windows is I first used an application called "Launch4j" to export from .jar to .exe . Then I made a copy of the folder I developed the game in so that all of the resource files are organized in the right way. Finally, I just put the .exe file in the main directory and it works.
My process for trying to make it run on MacOS was I first used an application called "AppMaker" on a Mac to export from .jar to .app . Then, I made a copy of the folder with the resources and put the .app file in, but it didn't open.
I noticed that if I exported a game without resources to .app and put it in the "Applications" folder on Mac, it worked, so I tried that with the resource game by putting my resource folders inside the actual Applications folder, but it also didn't open.
The only way I actually got a resource Java game to work on Mac was by not exporting it to a .app file at all and just leaving it as a .jar in the folder with the resources, but I really like how you can change the .app icon and it actually shows the file as an application.
Is there any way I can get my .app file with resources to run on MacOS?
I think that a FatJar might help you.
A Fat Jar is an archive which contains both classes and dependencies needed to run an application, here you have some info about it: http://tutorials.jenkov.com/maven/maven-build-fat-jar.html

Desktop.getDesktop().open(file) doesn´t work if jar needs an external file

I hava a jar file file.jar that needs a folder with pictures next to where the jar is. The program loads the pictures and shows them.
It works fine doing double click or java -jar file.jar, but if I try to open it with another java program doing
File file = new File("path/file.jar");
Desktop.getDesktop().open(file)
it´s not loading the pictures.
What can it be?
When you run java -jar file.jar, the jar file is in the current directory, and presumably so is the picture folder.
When you double-click a jar file, the code is run with the containing folder as the current directory.
When you "open" path/file.jar, the current directory is obviously different, because why else would you need to qualify the jar file name. Since you program replies on the current directory to find the picture folder, it fails.
Solutions, in my recommendation order (given the little I know of your code):
Include the pictures inside the jar file, then access them using getResourceAsStream.
Include the pictures in another jar file, have the manifest file of you main jar file include the picture jar file in the classpath.
Make sure the current directory is correct before try to "open" the file.

Adding .wav files and .ttf font files into executable jar file

I've created an executable jar file and added all the .wav, .ttf, .png and .class files into the jar. When I run the jar file it works perfectly when the jar file is contained in the folder where all of the other files needed are.
However I am trying to include all the files needed to execute my application properly inside the jar file. I used the command:
jar cvfe App.jar Main *.class *.png *.ttf *.wav
on windows command prompt. When executing the jar file outside the folder which contains all the necessary files listed above, only the image files and class files seem to be included in the jar file, the font files and sound files do not work and the GUI shows a default font and sound does not work either.
The stack trace on command prompt prints that all the files needed are being added when i execute the above command but it doesn't seem to work.
*****EDIT******
Heres how I am loading the font.
Font font = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream("C&LBOLD.ttf")).deriveFont(50f);
Heres how I am loading the .wav files:
File waveFile = new File("sounds//berlin.wav");
Most likely you are not loading those files through the class loader.
If you provide the code where you load those files we can check for sure.
[Edit] Thanks for posting the extra code - It's as suspected. You need to load the files using a class loader.
So, for example, where you have...
new FileInputStream("C&LBOLD.ttf")
You should have something like this instead...
this.getClass().getClassLoader().getResourceAsStream("C&LBOLD.ttf")
Java will then know to look on the classpath (and hence in the jar) for the resource.
You should use
Font font = Font.createFont(Font.TRUETYPE_FONT, this.getClass().getClassLoader().getResourceAsStream("C&LBOLD.ttf")).deriveFont(50f);
this is how you load resources located in the classpath (whether from a .jar or a directory). Just keep in mind this may not work in some containers if you are deploying the jar as part of a Web application, for instance.
It's also a good idea to check the input for null to make sure the resource exits.

How to create an executable jar folder?

I am trying to create an executable jar file that a user can just click on and execute the code. I have done some research but almost all of the examples are for single files. I currently have all my classes and images being used in my files in a folder. My images exceed the default java memory space and thus I have to run my code with -Xmx500m.
How can I create an executable jar that will go into my folder and run my program from there? Any help will be greatly appreciated!
Just use jar command to create the jar. You may need to link this type of file to Java so that it can be run by clicking on it in Windows. Something like this-
jar cvf test.jar <path to your classes / resources>
I am trying to create an executable jar file that a user can just click on and execute the code
Use Java Web Start to deploy it.
Put all the images and other media in Jar files mentioned in the launch file and they will be downloaded when needed. JWS also has facilities to adjust the memory allocated to an app.
I recommend using https://netbeans.org/ to build your project. Once built it will create the jar for you. You are also able to set compile settings such as the amount of ram you need.
See the following directions https://netbeans.org/kb/docs/java/quickstart.html#build

Generate Executable Jar file in LIBGDX

To be clear, i know there is a thread like this on stackoverflow and he uses eclipse while I use Intellij. Creating an executable jar in eclipse and intellij are different. So before downvote kindly consider my scenario. I created a simple java game using libgdx and want to export it in an executable jar. But before doing so, I experimented in creating a jar of the one in the tutorial video of official LIBGDX.This is the jar file Dload it here.. If you execute it opens then close again. But when i ran the project in intellij it runs without error.
This is the image i was expecting if i execute the jar file i created..
What is wrong when i tried to create a jar file?? Anyone?? Thanks
Just open your exported jar file like a zip archive and export your assets folder and libraries beside your jar file then test it again. Hope this helps.

Categories

Resources