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...

4

1 に答える 1

3

アプリケーションは log.txt 、JAR 内のログ フォルダー内に を保持する必要があります。

hereに記載されているように、JAR からファイルを抽出し、ファイルを変更し、アーカイブを復元することができます。ただし、現在ロードされているクラスが実行されている JAR を変更することは実際的ではありません。これは、進行中のログでは特に問題になります。代わりに、を使用して適切なパスを要求しJFileChooser、アプリケーションの .xml に保存しますjava.util.Preferences。一部のプラットフォームには、推奨パスがあります (例: ) 。

于 2013-01-26T10:25:05.707 に答える