1

I have an application built in Java, which, when a certain button is pressed within the application, another jframe should popup with a message. The ant file I made places the runnable jar into a folder within my Eclipse project. When the jar is built, and I run the jar in the folder it was born in, it runs fine. I can click the button that makes the popup show up and it does, indeed, show up.

The problem comes when I move the jar out and say, onto the desktop. Then running the jar starts the application, but pressing the button does nothing (no popup).

Now, I know I had issues including some image resources before, and had to use getResource() etc. I do not see why I would have to do anything like this since all of the "resources" are just .class files which are specified in the build path. I mean, all the app should be doing is creating a jframe...

EDIT: adding build.xml EDIT: slimmed build.xml down -- I think the problem is in the building of the JAR.

<!--Creates the deployable jar file  -->
<target name="jar" depends="compile">
    <echo>"Making Deployable Jar..."</echo>
    <jar destfile="${shipping.dir}/POSsystem.jar" basedir="${build.dir}">

        <fileset dir="." includes="${imgs.dir}/**"/>
        <fileset dir="." includes="db/**"/>

        <manifest>
            <attribute name="Built-By" value="${user.name}"/>
            <attribute name="Main-Class" value="${main-class}" />
            <attribute name = "SplashScreen-Image" value="${splash-screen}" />
        </manifest>

    </jar>
    <echo>"Success Making Deployable Jar..."</echo>
</target>

EDIT 3: Added output from running via the command line.

C:\Users\Matt\Desktop>java -jar POSsystem.jar
LOG COULD NOT BE CREATED!
java.io.IOException: The system cannot find the path specified
        at java.io.WinNTFileSystem.createFileExclusively(Native Method)
        at java.io.File.createNewFile(Unknown Source)
        at pos.log.GeneralLog.beginLog(Unknown Source)
        at pos.main.POSsystem.main(Unknown Source)

This is the first error. The application should keep a log.txt, within a log folder, within the JAR. You can see in the build.xml (first thing posted) that I add the log folder to be built in to the JAR, which is fine, but the path in my log.java code will obviously give me problems since it's a hard-coded path. So my question here is: How do I include a path to the resource in a JAR file. I know that to include an image I would do something like: javax.swing.ImageIcon(getClass().getResource("/images/pos_header_icon.png")); but I'm not sure how to access a file location...

Matt
  • 5,408
  • 14
  • 52
  • 79
  • 1) without pertinent code I'm not sure how we can play the "guess the bug" game and win. 2) You shouldn't even be popping up JFrames. That is what JDialogs are for. – Hovercraft Full Of Eels Jan 25 '13 at 23:54
  • Thanks for the reply. I didn't intend you to play "guess the bug". I'm trying to figure out if there's a clear reason that this doesn't work. Posting code I didn't believe would be helpful to anyone in this case. Also, can you provide a reason for not using a JFrame as a popup? In this case it is a form that the user would be filling out. – Matt Jan 26 '13 at 00:01
  • Sure, the JFrame is not dependent on the underlying window and that is bad window behavior. You rarely want to launch two separate windowing applications but rather would want to occasionally launch a dialog window that is dependent on the underlying window which is just what a JDialog does. It also gives you the flexibility and power of being either modal or non-modal, something a separate JFrame can't do. – Hovercraft Full Of Eels Jan 26 '13 at 00:04
  • 2
    And yes, you have a bug but without pertinent code, preferably an [sscce](http://sscce.org), I doubt anyone will have much of a chance of being able to help you. I hope that I'm wrong of course. – Hovercraft Full Of Eels Jan 26 '13 at 00:05
  • Great, thanks. I suppose I could include code from the build.xml. I will edit my post to reflect this. – Matt Jan 26 '13 at 00:13
  • 1
    1) What is your question? 2) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) 3) Run it from the command line (e.g. `java -jar the.jar`) and report any output. – Andrew Thompson Jan 26 '13 at 02:40
  • Thanks Andrew. I didn't think about running the jar via command line. I am getting a few errors, such as my db doesn't exist (which is accessed when the user clicks the button I was mentioning above). I'll edit my post with new info, but I do include the db files (see the part of my build.xml above). – Matt Jan 26 '13 at 02:54

1 Answers1

3

The application should keep a log.txt within a log folder, within the JAR.

As noted here, it is possible to extract a file from a JAR, modify the file, and restore the archive; but it is not practical to alter the JAR from which currently loaded classes are running. THis would be a particular problem for an ongoing log. Instead, solicit a suitable path using JFileChooser and save it among your application's java.util.Preferences. Some platforms have recommended paths, for example.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thanks trashgod. I did something similar; I just made a directory under "user.home" where I will be storing data like log data and db stuff, rather than accessing the JAR. I think it's pretty much what you are recommending, though I don't give the user the option to choose the location. – Matt Jan 26 '13 at 21:03
  • I will accept this answer, since my original problem had to do with the location of the db, log etc. – Matt Jan 26 '13 at 21:08