How to export image files into runnable JAR with eclipse [duplicate] - java

This question already has answers here:
Including Images with an executable jar
(2 answers)
Closed 8 years ago.
Title speaks for itself. I've made a clone of space invaders which uses several image assets. How do I tell eclipse to export the image files into the runnable JAR so they can be used by the program? I'm using eclipse europa.

As Mr. Anderser pointed out, make sure to read them as resources when inside of a jar, not files.
This might help.
Toolkit.getDefaultToolkit().getImage(getClass().getResource("images/imageFile.png"));

Images inside jar files must be read as resources and not plain files.
You can create a new sourcefolder, e.g. named "images", and put your images in there.

Related

Packing external files into jar and then allowing code to perform operations on these packed files [duplicate]

This question already has an answer here:
Executing a python file from within JAR
(1 answer)
Closed 5 years ago.
So I'm using IntelliJ IDEA and I've got my structure as follows:
So firstly, I want to be able to package the res folder with all my scripts inside the jar, and then when I run my program it can access the python scripts from inside the jar itself.
So far when I try running things on res/test.py it can't find the file.
EDIT: I've taken a look at an article which suggests getting the temporary files location using System.getProperty("java.io.tmpdir") and maybe copying the files to there using Class.getResourceAsStream("/path/to/file")
I'm not sure however if Class#getResourceAsStream is suitable for getting it from inside the jar to write it to the temporary location.
The code works when you check the file exists prior to executing the Process:
File f = new File("res/test.py");
System.out.println(f.exists()); // true

What do I put as a file's path if I want to use it in a program that's on several computers? [duplicate]

This question already has answers here:
How to includes all images in jar file using eclipse
(4 answers)
Closed 6 years ago.
I have an image file which I want shown as a JLabel in a JFrame for a program that will be on several computers running the same code. The image would not be on the computers already but would somehow be stored as a program file. The computers would all be windows. To insert the image a file path has to be given, but I'm not sure what this will be considering the computers are all different.
I have done this before on a different language by having the program find the program's directory and doing some string manipulation, but I have not been able to do this yet on java and would like to know if there is a better way.
Here is the statement that the path has to go into:
lblTitleBG.setIcon(new ImageIcon(file path goes here));
lblTitleBG is a JLabel.
First you create a source folder inside your project using eclipse or your preferred IDE. That will create a physical folder in your project folder that you can navigate in and move your files.
Let's say you have Project1 where you create Files_Folder. Now you navigate to the Project1 folder with window explorer and paste the files you need inside the Files_Folder and in the bin folder too. After this, just go back to eclipse and refresh/clean project. Your new moved files will appear in the Files_Folder in eclipse explorer too.
Just use lblTitleBG.setIcon(new ImageIcon("Files_Folder/image.png")); after following all the steps and you will have portable code.
Try to pay attention to steps and everything will work good. And finally, when you export your jar, don't forget to keep the Files_Folder in the same location to the jar, in order to find and use the needed files.

Why doesn't Eclipse export images? [duplicate]

This question already has answers here:
Eclipse exported Runnable JAR not showing images
(8 answers)
Closed 7 years ago.
I am writing a simple game in Eclipse (Java), it requires some images. It works fine in Eclicpse, but when I export it (as a runnable jar) there are no images (just empty places instead of them).
I have a source folder called "images" in the project, and I adress the images this way: "images/name.png". THe images are exported right into the jar.
I have tried placing images folder in an resource folder, still didn't work. I've also placed the images in different places in the jar, didn't work.
How do I make Eclipse export the project correctly?
(I'm just a begginer, so I don't know which information should I post)
If you want to include and access resources as images or configuration files from a runnable jar, you have to place them in your source folder to include it. Alternatively you could provide the full path to the resources, which of course makes it difficult to port to other systems...
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("file/test.xml").getFile());
http://www.mkyong.com/java/java-read-a-file-from-resources-folder/

Exporting Java game with Eclipse doesn't work [duplicate]

This question already has answers here:
Add image to JAR Java
(7 answers)
Closed 8 years ago.
I am trying to export my Java project using Eclipse's function but when I run it, it only shows the background, no external files. I used customized Checkboxes and Button, but they don't show up. Running it from Eclipse works perfect. This is the hierarchy for my Game:
.settings(folder)
bin(folder)
src(folder)
.classpath
.project
some .png files
This is how it looks in eclipse. I don't know what is with the red exclamation mark on Game.
These are the steps I took in creating the runnable JAR file:
I added my pictures to the Build Path (Skipping this step has the same effect)
I clicked Export - Runnable JAR file:
Then:
When I clicked finish, I had no warnings:
Why did you put all those images as referenced libraries? You should just put them in the src folder.
Any way, have you specified the main class when exporting the executable jar file?
Can you explain what happens when you try to execute the generated jar file?
These warnings may not be related to problem. But if you show them, it may be easy to check.
If I didn't get it wrong, Eclipse will only pack things in bin to the jar file. So you got two options:
Leave things as it is, but when deploying, put the images in the same folder with the jar file.
Put your images in src (so it would be copied to bin and hence packed) and change your way to access your image: get the URL with getClass().getResource() and read from the URL with something like Toolkit.getDefaultToolkit().getImage().

is there a way to know java packages in a jar file? [duplicate]

This question already has answers here:
How do I find the packages defined in a jar?
(5 answers)
Closed 8 years ago.
Is there any way to know the packages of a .jar file as I want to use "gtranslateapi-1.0" but not getting the package or class names in it.
I have also added it to my libraries in netbeans 8.0
You can see it here: https://code.google.com/p/java-google-translate-text-to-speech/downloads/list
please help, thanks in advance !!
jar is just a zip.so if you want to know what is packed into a jar file, you may unzip it (using either your favourite zip tool or jar itself e.g jar -t to list the contents). hint jar without args gives you a list of options
in netbeans you can easily see packages and classes .or you can rename .jar to .zip and open in compress program like winrar
in netbeans you can expand jar easily.add jar to libries and expand it .this is your jar file

Categories