I am creating a Java app for some serial port communication and within the app’s folder I have two resources that need to be present at run-time. The first is an image that is being used as a splash screen and the other is a configuration file that needs to be read as the program starts.
Here is an abridged version of the output from the “tree” command in Linux, the two files that I need to be referencing are “commandSet.config” and “splash-screen2.png”
.
├── bin
│ ├──...
├── commandSet.config
├── app-manifest.txt
├── splash-screen2.png
└── src
├── events
│ └── InterfaceEvents.java
├── models
│ ├── Command.java
│ └── Phone.java
├── operations
│ ├── Application.java
│ ├── ...
└── views
├── CallDialog.java
├── SplashScreen.java
└── Window.java
I am currently referencing in the following lines:
BufferedReader in = new BufferedReader(new FileReader("commandSet.config"));
and
JLabel image = new JLabel(new ImageIcon("splash-screen2.png"));
This works fine when I am running it from Eclipse, but as soon as I export to a runnable JAR the files are not moved and therefore aren’t correctly referenced. I have tried moving them into the “src” folder, which resulted in them being archived within the JAR, but I still couldn’t reference them.
I’m pretty new to the concept of exporting Java projects, so maybe I have missed something obvious. If someone could show me the best way to do this and the best approach for future project file systems, I would be very grateful. Cheers!
Ok, I mananged to crack this and thought I’d drop an answer for others in the future. I basically followed “duffymo’s” guidelines, here’s what I did:
Cheers!