How to export a a jar file correctly? - java

I mean I tried to export my java game like this : EXPORT>Jar File but if I do this it doesn't start.
And if I export to executable jar file it doesn't export my resources into the jar file.
I mean if I play the game in eclipse the sound works. But if I export to executable jar file it doesn't work. I guess it is not exporting the sound too.
This is the code I tried to use to launch the jar file :
java jar -cvfe ProjectZero.jar Main.Launcher Main.Launcher.class

Here are two solutions for solving this problem, hope this is clear enough:
Solution #1 - You want your resources outside of the JAR file
Just copy/paste the folder containing your resources in the same folder containing the JAR file. (Make sure the directory matches pathes mentioned in the application.)
Solution #2 - You want your resources inside the JAR file
If you want the resources to be directly included in the JAR file, you could use the function getResource() to get the images/sounds. Then make sure that resources are visible in both: "/src" and "/bin" folders.
For example, if you have the following application code:
ImageIcon myIcon = new ImageIcon(this.getClass().getResource("/resources/icon.gif"));
your file should be visible in:
/myApp/src/resources/icon.gif
/myApp/bin/resources/icon.gif
Then you can export your application as a JAR file, it will contain the resources.

Related

Images and text file doesnt show in the exported runnable jar file

I am working in Java Swing application. I am referring image in my program and it work fine when I run it in Eclipse but when I run it after exporting it doesnt show those files.
My directory hierarchy:
Code 1:
Code 2:
While exporting the jar I have selected
Library handling: Package required libraries into generated jar.
after converting jar to zip, the zip file contains all the resources folders in it.
and also classpath file contains resources folder included.
I have referred many answers here but nothing helped me.
Reffered Questions :
Exported JAR Won't Read Image
Picture inside .jar file wont work when I export it
Exporting Images with JAR in Eclipse (Java)
Eclipse exported Runnable JAR not showing images
Java images not appearing in JAR file
Eclipse exported Runnable JAR not showing images
Try new ImageIcon("/res/images/icon.png") or new ImageIcon("/res/images/icon.png") depending on where you can find the image in the jar file. Because I think without the starting "/" it will try to find it outside the JAR file.
If this is not working then you can try loading the file with [yourclassname].class.getClass().getResourceAsStream(...)

Exporting resource folder with jar

I have a folder called "resources" in my Eclipse project. It's used as source folder. Everything works well compiled from Eclipse. I wanted to try it on Ubuntu so I exported it to runnable jar and tried to run but it gave me FileNotFound Exception. I checked the file structure inside the JAR and there's no "resources" folder, only files that should be inside. I'm accessing files with
File file = new File("resources/hotkeys.ini");
I also tried
InputStream in = getClass().getClassLoader().getResourceAsStream("/resources/hotkeys.ini");
but it gave me null.
How to force Eclipse to export the folder along with jar?
When you use "Export" functionality, you need to select all resources that need to be exported (it is visible at the top of the Eclipse export wizard window).
You can find the answer here as well: Java: export to an .jar file in eclipse
You can open the jar with 7zip or Winzip, and check the path.
Try
getClass().getResourceAsStream("/resources/hotkeys.ini");
(Or try removing the first / when with getClassLoader().)
Especially check the case-sensitivity of the paths. As opposed to Windows, inside a Jar it is case-sensitive.

how to place properties file in a directory in the classpath ahead of .jar in eclipse?

I want to place ABC.properties file in a directory in the classpath ahead of cmbview81.jar .
cmbview81.jar already has ABC.properties file in it. I extracted the file made some changes and made new ABC.properties file and now want to set its path ahead of cmbview81.jar file so that application will not point to the file which is in cmbview81.jar file.It should use new ABC.properties file.
I tried solutions provided here How to place a file on classpath in eclipse? but didn't work.
Put it in a separate Jar file and see to it that this Jar file is included in front of the existing Jar file in the classpath specification. The order of specification of the Jar files in the classpath is significant (it's the same idea as directly using the CLASSPATH variable).
You could also put it in your src somewhere, but just make sure then that your src folder occurs before the Jar files in the Order and Export tab of the Eclipse Java build path configuration.

Add resources into a jar upon building

I want to add DLL's, images, textfiles etc to my project as resources so when I export it, the jar contains the resources so they can be used. I am using eclipse.
Problem is I have no idea how to add it. I've tried adding DLLs/pics to the src folder in the project, but when I export the jar, it is not located there
I've looked at How to make a JAR file that includes DLL files? but it only explains how to extract it, not how to add it to the project and build.
EDIT: I am using an applet to open the jar by the way, sorry for missing it!
Cheers
How are you opening the file in java?
Class.getResourceAsStream(name)?
If you are packaging the code in a jar, then you need to use that command. (as opposed to new File(name), which will get the file in the same directory as your jar)
If the file is not physically in your jar, you can check by changing .jar to .zip and extracting it, then check out this doc http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/javac.html
Usually in an eclipse project, the src folder is the wrong place to put non-sourcecode-content.
You should try moving to maven as your build system, as it is highly customizable and provides you with folders inside your project for exactly that purpose. (src/main/resources)

Access .properties file at same level as working directory

I am working on a Java project that I want to deliver to my client as a .jar file. However, I want to allow the client to be able to change the parameters of the program without having to recompile or recreate the .jar. Basically, I want to be able to load .properties files from classes inside the .jar but locate those .properties files outside of the .jar and even outside the working directory.
I have been testing my attempts inside Eclipse, which might be causing some of the problem but I don't see how at the moment. My setup is a follows. I have one project that contains a few classes that I build a .jar file from. I have a .properties file that is used to create a ResourceBundle whenever a class for the .jar is created. I specify that an additional directory, "conf/", be included in the .jar classpath within the .jar manifest.
Once the .jar file is built, it is copied to the lib/ directory of another project which I am using for testing. This test project includes the .jar file as a library ("Add External Jars..") in the Java Build Path. The .properties file is located the conf/ directory which is at the same level as lib/, src/, and bin/ but I am unable to accces it there. The only way I have been able to get it to work is to locate conf/ under src/ (and bin/) but I would like to be able to use it up one level. Is this possible?
Here's the entry in the .jar manifest file...
Class-Path: ../conf/
Here's the ResourceBundle call that I tried (didn't work)...
rb = ResourceBundle.getBundle("..conf.BaseProject");
Here's the directory structure that works now (names have been changed to protect the innocent)...
/Project
/Project/bin
/Project/bin/conf
/Project/bin/conf/BaseProject.properties
/Project/bin/TestClass.class
/Project/lib
/Project/lib/BaseProject.jar
Here's the directory layout I want (again, file names not important)...
/Project
/Project/bin
/Project/bin/TestClass.class
/Project/conf
/Project/conf/BaseProject.properties
/Project/lib
/Project/lib/BaseProject.jar
You can get it using the Classloader.
I like Spring's ClassPathResource

Categories

Resources