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/
Related
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.
This question already has answers here:
Is it really impossible to protect Android apps from reverse engineering?
(10 answers)
Closed 7 years ago.
I am Android developer.
I have created a .jar file from Java project with below steps
Right Click on project and select export option.
Selected jar file from Java folder
Clicked on finish. Then mysdk.jar is created on desktop.
When I pasted this jar in lib folder of Android Project. When I selected(ctrl + left click from mouse) any method presented in jar from AndroidProject/src/demo.java , It is showing source code of .jar file.
My requirement is to give this jar to Third Party Company who should not read my code.
So, I want to hide my source code of jar. but how ? Please some one help me.
I am using Android Studio.
Thank you in advance.
Extract the created jar file and check if only contain the class files or source file. Actually it shouldn't contain source files in jar files. If it contains , make use of jar command in shell/command line to create jar file and deliver it. Also check the new project is linked the previous project because both exists in the eclipse.
jar -cvf <jar file name> files to be archived
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().
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.
I have been browsing Stackoverflow all day looking for how to do this and I have not been successful yet
I am packaging a quick game I made into a executable jar but I didnt reference the images correctly I just referenced the files
background = ImageIO.read(new File("wood.jpeg"));
I have my classes in src default package
Im not sure where I should add the images or if I have to add it to the build path
or correct way of adding the images to the build path in the newest version of eclipse
Files in a Jar are not files in the sense of a file on disk. They are simply a (possibly) compressed stream of bytes.
Java makes it easy to extract these "resources" from Jar files through the use of the ClassLoader
background = ImageIO.read(getClass().getResource("/wood.jpeg"));
Should work...
This will return a URL which ImageIO can use to load the resource.
You could also have a read of
Classpath resource within jar
Jar get image as resource
Load a resource in Jar
And I could list some more. So, yeah, it gets asked a lot ;)
Try using Constructing Runnable Jar using the Eclipse.
R_Click on the Project in the Package Explorer --->
Export ---> Runnable JAR file --->
Select the option of Package required libraries into generated JAR