I'm trying to export my Java game as a Runnable Jar which works, but when I open it up I get an error that my files aren't present.
Picture of error:
Picture of res folder in eclipse:
The game runs perfectly fine in Eclipse.
I have added the res folder as a source folder.
Thank you!
The contents of a jar file are not java.io.Files. Understand that you don't actually care about the files, you want their contents, so start using getResourceAsStream()--it works for both cases pretty transparently.
Related
I have some images and xml files inside my project folders which I want to read. For example, my code is inside src/app/MyClass.java. The files I want to read are in res/filename.png, res/filename.xml.
So if I wanted to give my project an icon, I would do this (works fine if running through Eclipse):
primaryStage.getIcons().add(new Image("file:res/icon.png"));
The problem with this is that when I run it through the .jar file, the image does not appear. Same thing with other files. It can't find the xml file through the jar file.
I tried this:
InputStream input = getClass().getResourceAsStream("res/icon.png");
primaryStage.getIcons().add(new Image(input));
But Eclipse says that input is null. What am I doing wrong? Any help would appreciated!
There are some possibilities as to why this is the case. The first of which is you are running a JAR and not an Executable Jar File. Another possible reason why the program fails to display images is that when exporting you are not clicking the extract required libraries button as shown below. If neither of these are the case comment below.
Try creating a source folder named resources (right click project > New > Source Folder) and put everything in there. You can now do getClass().getResourceAsStream("/file.ext") and it shouldn't be null.
If there are any other errors after you create your jar file (following the picture above), you can run this through command line/terminal and it should show you any errors you may have:
java -jar jarfile.jar
Well I have these .java files in JCreator.
When I compile them in JCreator, it's fine, all files and folders are read and no errors occurred. But, I need to compile them in an Eclipse project to pass it to my professor. Then the problems started.
I added the .java files in a new Eclipse project and into a new package inside the
src folder
Then after that, I made a new Folder in the project called
res
and added it to the build path as a source Folder and added the files and folders in it.
>Use as Source Folder
All sub-folders are fine same name and stuff, Refreshed the project a few times, checked the build path, but when I execute the program, It says:
java.io.FileNotFoundException: sounds\bgmusic.wav (The system cannot find the path specified)
from this line:
InputStream test = new FileInputStream("sounds\bgmusic.wav");
Same folder name, same file, added it to the Build Path and stuff. Also, I repeat, It does not find ALL the files in the resource folder: Pictures, Sounds Fonts and other stuff. I already made some projects in Eclipse and I had successfully added pictures and sounds in them. But what's the problem with this?
Thanks for the help!
Try this:
ClassLoader.getResourceAsStream ("sounds\bgmusic.wav");
I hope it resolves your issue.
You can also refer below link:
http://www.javaworld.com/article/2077352/java-se/smartly-load-your-properties.html
I've run .jar files before, but I've encountered a "different" situation, and I'm not sure what to do. I'd appreciate if someone could help me out.
Previously, I programmed with Java 6 and Eclipse Juno exported all my programs to runnable jar files. I'd get a .jar file that I could run by just double clicking on it. The files always looked something like this (note the jar file icon):
Recently, I wrote a program in Java 8 with Eclipse Luna (Release 4.4.0) and exported it to a runnable jar file, and I got something different (note the different file icon):
It no longer runs when I double click it. Instead, my computer uncompresses the jar, as it would a zip file. I tried running it from terminal. I cd'd to the directory and typed
java -jar graph3D.jar
I got the following error message:
Error: Unable to access jarfile graph3D.jar
After uncompressing the jar file, I found a folder named META-INF with the manifest file, MANIFEST.MF in it. It was the only file that seemed to resemble an executable file. Do I have to do something with that?
Could someone explain how I can run the second jar file graph3D.jar? Is it something new with Java 8, or something different about Eclipse Luna, or something else?
(Both programs run fine in Eclipse, by the way)
Thanks for your time and help.
Edit:
Below was the dialog box Eclipse displayed if anyone is interested.
Selecting "Use .jar;.zip" makes the filename "graph3D.jar;.jar;*.zip" .
Selecting "Use .zip" makes the filename "graph3D.jar;*.zip"
Selecting "Cancel" doesn't let you go forward.
You'd have to manually delete the extra file extension.
Somehow when you exported the file, the filters for the file dialog box (*.jar;*.zip) got attached to the filename, which is graph3D.jar;*.jar;*.zip, not graph3D.jar. Java can't find it because it doesn't have the name you supplied. Rename the file and pay close attention next time you export; either you fat-fingered something, or you're triggering a significant bug that needs fixing.
I recommend that you will access the build folder after you've built your project on the IDE under your project folder (in your workspace) and copy both the libraries folder and the .jar and post them wherever you want the program to be "installed", you'll then have an executable jar that should run smoothly without problems, just as I said don't forget the lib folder.
I think there is nothing new in Java 8 related with the running jar, I guess you need to check the the Eclipse export issues, it seems your classes are missing from your second jar file.
I've been working on a processing application using ControlP5 and Twitter4j. I want to have my project run from a single jar file from any operating system. Basically I want to package up my application. My application uses images. I've been browsing for more than an hour, but I cant find how to do this. Any suggestions?
using
processing 2
twitter4j3
Thanks in advance!
I dont know if you can directly do it from the Processing IDE however, if export your sketch to a Java applet then locate the .java the the sketch folder you can use this in conjunction with Eclipse to export to a jar file.
So, I know that this post is very old but if you are still looking for a solution, or to other people that see this thread, it's relatively simple.
Export the project
In the folder with the exported project (something like application.windows64), navigate to lib and find core.jar and project name.jar (you need to have file name extensions visible)
Rename the files to .zip files
Extract core.jar to whatever folder
Extract project name.jar into the same folder (make sure you don't do it into a subfolder)
Click yes if it asks if it wants you to replace a file (if it doesn't you extracted the files incorrectly)
Delete core.jar and project name.jar
If the project uses images, move them into the same folder as all the other files
Select all of the files in the folder, right click, hover over send to and select compressed (zipped) folder
Rename the .zip file to name of project.jar
This might be old, but i still find other posts about it on processing forums.
This is the best way to run processing project as a jar file.
When exporting application, you will always end up with a lib folder inside exported application(whether for Linux and Windows). For windows, open command prompt(or power shell), you can use right-click+shift and then click on open power shell here.
After that you can run the following command.
java -classpath lib\* DisplayDepthStream
Now DisplayDepthStream is the name of sketch file.
To explain the command, -classpath lib\* tells java to add everything under lib directory to the class path. And DisplayDepthStream is the name of my main class.
Hope this helps~!
Chears
I would like to export my Java program, but I have run into problems. It exports fine but my resource files like my res folder and my sound folder aren't there. They are located in the project directory, so I don't know if that should be a problem. I am currently just using ImageIcon().getImage() to get my pictures and that works fine but when I export and run it the program runs but it doesn't have any pictures. Any help would be greatly appreciated
The problem is that once the images/sounds are inside the jar you must access them as a resource.
Image image = new ImageIcon(this.getClass().getResource("/image.gif")).getImage();
Make sure the images are really inside the jar opening the jar with winrar or similar if not add the folders as source folders in your buildpath.