1

Well, I tried so many ways to create executable jar, but after 4 hours I give up. I am trying to make executable jar file with my project where main class is in the controller. Basically I tried with export/runnable JAR file with various options including generating of ant script . I always have similar errors in terminal

When I'm running that with terminal with

java -jar fruitStore.jar

it says

    bmj@OZ:~/Documents$ java -jar fruitStoreApp.jar 
java.io.FileNotFoundException: customers.ser (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileInputStream.<init>(FileInputStream.java:93)
    at model.Shop.loadCustomers(Shop.java:250)
    at model.Shop.<init>(Shop.java:121)
    at model.Shop.getInstance(Shop.java:140)
    at model.Model.<init>(Model.java:12)
    at GUI.CustomerMainPanel.<init>(CustomerMainPanel.java:24)
    at GUI.MainMenuGUI.<init>(MainMenuGUI.java:50)
    at GUI.MainMenuGUI.getInstance(MainMenuGUI.java:35)
    at model.View.<init>(View.java:49)
    at model.Controller.<init>(Controller.java:42)
    at model.Controller.main(Controller.java:805)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
java.io.FileNotFoundException: users.ser (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileInputStream.<init>(FileInputStream.java:93)
    at model.Shop.loadUsers(Shop.java:210)
    at model.Shop.<init>(Shop.java:122)
    at model.Shop.getInstance(Shop.java:140)
    at model.Model.<init>(Model.java:12)
    at GUI.CustomerMainPanel.<init>(CustomerMainPanel.java:24)
    at GUI.MainMenuGUI.<init>(MainMenuGUI.java:50)
    at GUI.MainMenuGUI.getInstance(MainMenuGUI.java:35)
    at model.View.<init>(View.java:49)
    at model.Controller.<init>(Controller.java:42)
    at model.Controller.main(Controller.java:805)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
java.io.FileNotFoundException: suppliers.ser (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileInputStream.<init>(FileInputStream.java:93)
    at model.Shop.loadSuppliers(Shop.java:190)
    at model.Shop.<init>(Shop.java:123)
    at model.Shop.getInstance(Shop.java:140)
    at model.Model.<init>(Model.java:12)
    at GUI.CustomerMainPanel.<init>(CustomerMainPanel.java:24)
    at GUI.MainMenuGUI.<init>(MainMenuGUI.java:50)
    at GUI.MainMenuGUI.getInstance(MainMenuGUI.java:35)
    at model.View.<init>(View.java:49)
    at model.Controller.<init>(Controller.java:42)
    at model.Controller.main(Controller.java:805)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
java.io.FileNotFoundException: stocks.ser (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileInputStream.<init>(FileInputStream.java:93)
    at model.Shop.loadStock(Shop.java:230)
    at model.Shop.<init>(Shop.java:124)
    at model.Shop.getInstance(Shop.java:140)
    at model.Model.<init>(Model.java:12)
    at GUI.CustomerMainPanel.<init>(CustomerMainPanel.java:24)
    at GUI.MainMenuGUI.<init>(MainMenuGUI.java:50)
    at GUI.MainMenuGUI.getInstance(MainMenuGUI.java:35)
    at model.View.<init>(View.java:49)
    at model.Controller.<init>(Controller.java:42)
    at model.Controller.main(Controller.java:805)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
java.io.FileNotFoundException: sales.ser (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileInputStream.<init>(FileInputStream.java:93)
    at model.Shop.loadSales(Shop.java:150)
    at model.Shop.<init>(Shop.java:126)
    at model.Shop.getInstance(Shop.java:140)
    at model.Model.<init>(Model.java:12)
    at GUI.CustomerMainPanel.<init>(CustomerMainPanel.java:24)
    at GUI.MainMenuGUI.<init>(MainMenuGUI.java:50)
    at GUI.MainMenuGUI.getInstance(MainMenuGUI.java:35)
    at model.View.<init>(View.java:49)
    at model.Controller.<init>(Controller.java:42)
    at model.Controller.main(Controller.java:805)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
java.io.FileNotFoundException: orders.ser (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileInputStream.<init>(FileInputStream.java:93)
    at model.Shop.loadOrders(Shop.java:170)
    at model.Shop.<init>(Shop.java:127)
    at model.Shop.getInstance(Shop.java:140)
    at model.Model.<init>(Model.java:12)
    at GUI.CustomerMainPanel.<init>(CustomerMainPanel.java:24)
    at GUI.MainMenuGUI.<init>(MainMenuGUI.java:50)
    at GUI.MainMenuGUI.getInstance(MainMenuGUI.java:35)
    at model.View.<init>(View.java:49)
    at model.Controller.<init>(Controller.java:42)
    at model.Controller.main(Controller.java:805)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Exception in thread "main" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: javax.imageio.IIOException: Can't read input file!
    at javax.imageio.ImageIO.read(ImageIO.java:1301)
    at GUI.WelcomeScreenGUI.<init>(WelcomeScreenGUI.java:33)
    at model.View.<init>(View.java:51)
    at model.Controller.<init>(Controller.java:42)
    at model.Controller.main(Controller.java:805)
    ... 5 more

I know where is the problem. It doesn't attach marked files on the picture to the jar

picture with needed files

My question is: How to add that files ? Can you please give me detailed answer for that. One condition. I can't change place of that files.

I read about some solutions with Maven which I would like to avoid due to lack of knowledge of Maven.

Thanks in advance for quick reply

Andrew_Dublin
  • 745
  • 6
  • 22
  • I don't understand your question. Do you need code when I run jar file in terminal by java -jar fruitStore.jar ? – Andrew_Dublin Sep 03 '14 at 15:20
  • post the code which is throwing the exception – SparkOn Sep 03 '14 at 15:22
  • Are those files being placed in the executable jar? Your first step is to make sure they are – Michael Wiles Sep 03 '14 at 15:26
  • Can you show the code where you're trying to read the file – Paul Samsotha Sep 03 '14 at 15:28
  • No, that is the problem. I attached picture which says which files I need also attach to the jar. Because of that project doesn't run from the jar – Andrew_Dublin Sep 03 '14 at 15:29
  • @peeskillet There are many classes which read that files. I can't change code - to many changes needs to be done. Project runs perfectly from eclipse. Pictures are not a problem. Txt files and ser files are causing problem because they are not attached to the jar – Andrew_Dublin Sep 03 '14 at 15:33
  • @MichaelWiles No, that is a problem. I attached picture in the post with marked files missing. Terminal clearly says that can't read files because they are not attached. Because of that project doesn't run from the jar. I basically don't know how to attach that in the executable jar because export method in eclipse not working – Andrew_Dublin Sep 03 '14 at 15:35
  • `Project runs perfectly from eclipse.` yes it will but when you export it to a jar the environment changes keep all your files under src/main/resources and try reading them using geResourceStream() – SparkOn Sep 03 '14 at 15:39
  • @SparkOn I can't change code at this stage. Is there other option ? – Andrew_Dublin Sep 03 '14 at 15:40
  • We need to see the code. It's either change the file locations or change the code. Since you can't change the code, we need to see it, so we can suggest how to change the files. At least how you are reading the files and the path you're using. Also are these files in your images being read by the "other" code that you can't change? Did you write this code? I don't get how this problem got so far, how you you have this many resources files and you are just now discovering that they can be read – Paul Samsotha Sep 03 '14 at 15:43
  • @peeskillet code is available on https://github.com/fcpteam1/project – Andrew_Dublin Sep 03 '14 at 15:45
  • For general info for exporting the files and reading, see the second example [from this post](http://stackoverflow.com/a/25636097/2587435). Create a `resources` folder for the files, then add the directory to the build path. Once you have the files on the build path, then use `InputStream is = getClass().getResourceAsStream("/file.ser")'` – Paul Samsotha Sep 03 '14 at 15:46
  • It's hard to tell what you can do, especially if you can't change the code. The code is reading the files from the file system. Application resources files that are in a jar should be read from the classpath, and not from the file system. – Paul Samsotha Sep 03 '14 at 15:51
  • @peeskillet Im doing presentation and to impress lecturer I thought I will make a website where I download project from. I didn't think there will be so large problem to create executable jar file from eclipse – Andrew_Dublin Sep 03 '14 at 15:55
  • Another thing too, if you want the files in the jar, you will not be able to write to them. – Paul Samsotha Sep 03 '14 at 15:56

2 Answers2

1

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

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Ye, it works well. I am wondering how to put it on the website and make it available to download & run after click. Thanks anyway for your help – Andrew_Dublin Sep 03 '14 at 21:08
  • Well that won't work for the simple fact you won't be able to write to the file. Can't write to file in a jar. So you can throw that idea out the window. It's not uncommon to have some download a zip, unzip it, and then the executable is inside the zip main dir. You could simple just create a folder, add the necessary files and jars in the same hierarchy as in your project. I don't see anything wrong with that. You kinda limited yourself from the start. Better to just make the best of it :-) – Paul Samsotha Sep 04 '14 at 02:58
  • Also what is the username and password I type in to login. I wan't to test out some theories. – Paul Samsotha Sep 04 '14 at 03:25
  • You have logins and passwords in the UserList file. Also your way works with terminal by java -jar <>, but when I want to double click that jar with a mouse it is not working. Do u have any idea how to change it ? – Andrew_Dublin Sep 04 '14 at 13:12
0

The easiest thing to do is to extract your existing JAR file, include those folders with the external text and ser files, and repackage your JAR.

  1. Make a copy of your original fruitStoreApp.jar somewhere in case things go wrong.
  2. To extract a JAR file from command line, run jar xf fruitStoreApp.jar.
  3. Copy your model folder (and whatever else) over.
  4. Remove your old fruitStoreApp.jar so it doesn't get included in the next re-jar step.
  5. To re-jar all your artifacts by running jar cf fruitStoreApp.jar *.

Using ANT

If you want to try this withant, you can include the fileset type in your jar task like this:

<target name="package">
    <jar destfile="fruitStoreApp.jar" >
        <fileset dir="classes"/>
        <fileset dir="model" />
        <fileset dir="images" />
        <fileset dir="GUI" />
    </jar>
</target>

This will jar all files in the classes, model, images, GUI folders together into a file called fruitStoreApp.jar. I am assuming that all your .class files are within the classes folder.

ivan.sim
  • 8,972
  • 8
  • 47
  • 63
  • well if you know how to do it with maven and it is not possible by export feel free to post something – Andrew_Dublin Sep 03 '14 at 16:05
  • @Andrew_Dublin You probably don't need `maven` unless you need to re-compile/re-build your codes, which will involve converting your project into a `maven` project. I have updated my answer with a simpler solution using straight up the `jar` command from command line. Hopefully, that will help. – ivan.sim Sep 03 '14 at 16:29