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.
Related
I'm developing a project in Eclipse with SWT, where the interface is shown in a browser and for that, I use the XULRunner (Firefox) instead of using the standard browser.
I thought to compile the jar with the xurlrunner folder inside, so I created a source folder called "plugins" and copied the xurlrunner folder inside and got the path:
URL xulrunner = getClass().getClassLoader().getResource("xulrunner");
File file = new File(xulrunner.toURI());
System.setProperty("org.eclipse.swt.browser.DefaultType", "mozilla");
System.setProperty("org.eclipse.swt.browser.XULRunnerPath",
-> file.getAbsolutePath());
It worked, but only when run from within the Eclipse before exporting, however, the big problem is that when I export for a jar executable, the JVM can not load, of course, the reason is that file.getAbsolutePath() could not point to into the jar.
For images, I use getResourceAsStream() and it works very well once compiled, but when it is to transform a folder path inside the jar to String is that it is difficult.
QUESTION:
Is there any other way I can point to a folder into the jar that can be turned to String?
PROPOSAL
if there is no a way to keep the xurlrunner folder into the jar, I'll create an installer apart to extract it from a zip to the same location of the program installation and dynamically point to it.
SWT expects the XULRunnerPath to point to a regular filesystem folder. Thefore the entire xulrunner folder must be extracted into a regular folder before setting the system property.
I am trying to export my app into jar using intelij Idea artifacts. I export everything except for resources (14,5GB is in my opinion too much for .jar file) so I have put /res folder inside same folder as jar.
Right now my structure looks like this:
/SAR.jar
/res/colors/
/res/icons/
/res/map/
However I cant access this resources. I am using the following code to get path:
dir = new File(getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath() + "/res/icons/");
It works when running from IDE and it prints correct location. But when I build an artifact and run it through cmd I get the null pointer and it prints this path: D:/JavaProject/SAR/out/artifacts/SAR_jar/SAR.jar/res/icons. But my resources are not inside jar file they are only in same directory.
Any ideas? I have done only android projects in path and I simply cant get this resources system with jars to work.
Thanks in forward
You are trying to access the ressources with a path dependent of your source code location, which is obviously not the case since you separated the code (or bytecode in the case of the jar) and the ressources. You must access them with a path relative to the jar or an absolute path.
I think you just need the following path (if the jar was ran from the directory containing the jar) :
new File("res/icons/icon.png")
When I'am trying to export my project to a executable .jar-file then configuration files like hibernate.cfg.xml or log4j-properties are not exported into the jar. I have to add them manually to the archieve. The files are located in the /target/ folder in the root folder of the project.
How do I get Eclipse to export the config files too?
Just create a resources folder parallel to your source/src folder, and keep your configuration files in it.
If you use the Eclipse approach:
Project >> Export >> Java >>
Runnable JAR File >> Package required libraries into generated JAR
Only the class files get exported in the jar and - if this option is selected - the linked libraries.
You can, however create an additional source folder (name it resources or config or whatever fits your case) and copy your files into this one. This directory will then also be part of your generated jar file.
The problem of non-class files not showing up in the exported jar file may be due to Eclipse hiding the output folders. That is what happened to me. The problem was that the bin folder for a project my project depended on was hidden by Eclipse. Fixed that by using the Project Explorer to get Eclipse to make the output folder visible.
See what can I do to make display the bin folder on eclipse?
Using Eclipse Oxygen, fixed it by:
-- Selected the project with the hidden output folder in the Project Explorer
-- Clicked on the triangle in the Project Explorer
-- Clicked on Filters and Customization
-- Make sure the Filters Tab is selected.
-- Uncheck Java Output Folders
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.
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)