Creating Runnable Jar with external files included
Here's what you should do instead:
Put that file back in your jar file. Use class.getResourceAsStream()
to read it instead of File
and FileReader
. Here is an explanation of how to do that: How to really read text file from classpath in Java
Creating Runnable Jar with external text and ser files included
After going through your code, I see you have your file names set and you are using FileInputStream
which reads from the file system.
private String saleFile = "sales.ser";
...
public void loadSales(String inPutFile) {
try {
FileInputStream fileIn = new FileInputStream(inPutFile);
So what's going to happen is the program will look for sale.ser
in the current working directory. In your IDE normally the working directory is the project root, which is where your files are, that's why it works.
So since you can't change the code, here are some suggestions.
When you run the jar, you need to run the jar from the current working directory. When you export the jar export it to the project root. And when you run, make sure you're in the project root
Probably a better approach would be to write script to launch the program (which is pretty common). This will allow more flexibility to how/where you launch the app. I'm not the greatest at writing scripts so I'll just put the most basic, but you may want to may it more complex based on how you want to launch the app and locate resources. With the .bat file example below, the .bat file should also be in the project root
@echo off
setlocal
if "%JAVACMD%" == "" set JAVACMD=%JAVA_HOME%\bin\java.exe
if "%FRUITSTAND_HOME%" == "" set FRUITSTAND_HOME=%~dp0
cd "%FRUITSTAND_HOME%"
"%JAVACMD%" -cp "%FRUITSTAND_HOME%\lib\*" -jar "%FRUITSTAND_HOME%\fruitStoreApp.jar"
endlocal
You then (from the project root) simply run fruitStoreApp.bat
.
I tested it and it works. I just get FileNotFoundException
for the stocks.ser
sales.ser
and order.ser
, because... well they don't exist (in your project - at least not in your github).
Also looks like you're using bash so you will have write a simple bash script. I would create both .bat
and .sh
files, which is pretty common, to enable Windows and Unix users access
How to create a jar with external libraries included in Eclipse?
When you export your project as a 'Runnable jar' (Right mouse on project -> Export -> Runnable jar) you have the option to package all dependencies into the generated jar. It also has two other ways (see screenshot) to export your libraries, be aware of the licences when deciding which packaging method you will use.
The 'launch configuration' dropdown is populated with classes containing a main(String[])
method. The selected class is started when you 'run' the jar.
Exporting as a runnable jar uses the dependencies on your build path (Right mouse on project -> Build Path -> Configure Build Path...). When you export as a 'regular' (non-runnable) jar you can select any file in your project(s). If you have the libraries in your project folder you can include them but external dependencies, for example maven, cannot be included (for maven projects, search here).
How to Create a runnable jar that uses external jar files in dynamic locations
Pay attention that you can not use -cp and -jar at the same time. When you do java -jar, java expects the referenced jar to be declared inside the MANIFEST.MF file. I think your command line should work putting your jar inside "cp", and calling explicitely the main class on the command line.
Another solution if you are concerned with the size is to deploy your application with JNLP and compress your jars with Pack200.
Generating a Jar in Eclipse including external library
Eclipse has its own Jar export wizard for generate a runnable jar packed with required library or with the required library in a folder aside the jar.
Going in File ---> Export then choose Java - Runnable Jar
You can then choose how pack the jar and how handling libraries :
You can also save the ant script for later modification or use ...
Related Topics
What's the Meaning of System.Out.Println in Java
Writing in the Beginning of a Text File Java
Tomcat 7 "Severe: a Child Container Failed During Start"
No Idea Annotations Attached to the Jdk 1.8 (C:\...), Some Issues Will Not Be Found
Get Key Press Without Pressing Enter in Console
Java: CSV File Easy Read/Write
Why in Java Enum Is Declared as Enum<E Extends Enum<E>>
Getting Java.Lang.Reflect.Invocationtargetexception While Adding a Button to Layout
Drawing a Component to Bufferedimage Causes Display Corruption
How to Convert a Java Object to Xml with Open Source APIs
What Code Does the Compiler Generate for Autoboxing
How to Search in a List of Java Object
Parsing Dates of the Format "January 10Th, 2010" in Java? (With Ordinal Indicators, St|Nd|Rd|Th)