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
Related
I have three classes, one of them is called Main and contains a main method, and no package. The IDE I use is eclipse. If I click File > Export and then select Java > Runable JAR File I'd expect to get a file that I can double-click and that'll run my program. However, that's not the case. If I click the resulting file nothing happens. I'm very new to this whole thing, so my questions are: Is this file even supposed to be clickable, or am I missing a step to run it? If that's the case, how to I convert the file to a .exe file (I really don't get this manifest-stuff, so please explain in detail how that needs to be done)? Basically: How do I create an executable (executable as in "works if i just click on it") file from three classes (with eclipse if possible)?
Edit: It's a console app.
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.
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 have made a small program in Java that displays its .java source with a gui. It does not use FileChooser to do this. I am reading the .java sources with the aid of following statements
String resName = "/dev/classes/"+name+".java"
Scanner s = new Scanner(FilePrinter.class.getResourceAsStream(resName));
where name is the name of the .java file i.e. if the file is MyProg.java then name==Myprog. Of course my program is inside the dev.classes package
The thing is that when I export my project to JAR and include source files this works because source files reside inside the /dev/classes/ directory.
However, I haven't yet discovered a way to make my program run in Eclipse or from the command line without giving me exception.
And of course when someone tries to add those source files to be used automatically as resource files the process fails.
Can I somehow use the same code both when running from Eclipse and from the JAR? It must be something very trivial but because I am not Java expert I cannot see.
I found how to do it. Actually you either have to use Ant or Maven. However, this can be done in Eclipse as well as follows:
On the Eclipse Project Properties>Java Build Path you can choose on the bottom Default Output folder: <your_project_name>/src.
This causes class files be compiled in the same directory as the .java files and finally does what I wanted.
Thanks to #AndrewThompson for suggesting to try this