自包含JavaFX应用程序工作目录

时间:2015-02-03 17:56:49

标签: java eclipse windows javafx-2

我在使用自行打包应用程序的个人项目上遇到了麻烦:

我使用E(fx)clipse创建了我的应用程序,并且我正确生成了build.xml(添加代码以复制我的资源文件夹)。 我需要捆绑包将特定文件提取到特定文件夹(在app文件夹中)所以我可以调用一些executableandroid debug bridge代表我。)

当我使用可执行jar文件(安装后在app文件夹中)时,一切正常,但是当我使用已安装的可执行文件时,我无法获取可执行文件的工作目录:

当我“本地”运行时,我可以使用它:System.getProperty("user.dir"));

但是当我运行exe时,没有任何反应,我只是无法得到这个字符串。

更具体地说,这是我的代码:

public void start(Stage stage) {
    Circle circ = new Circle(40, 40, 30);
    Group root = new Group(circ);
    Scene scene = new Scene(root, 400, 300);

    stage.setTitle("My JavaFX Application");
    stage.setScene(scene);
    stage.show();

    Stage dialogStage = new Stage();
    dialogStage.initModality(Modality.WINDOW_MODAL);
    dialogStage.setScene(new Scene(VBoxBuilder.create().
    children(new Text("Working Directory = " +
          System.getProperty("user.dir"));
    "), new Button("Ok.")).
    alignment(Pos.CENTER).padding(new Insets(5)).build()));
    dialogStage.show();

}

当我使用e(fx)clipse(或可执行jar)运行应用程序时,我得到了工作路径,但是当我使用生成的.exe时,没有任何反应,对话框没有出现

我正在使用JavaFX 2.2JDK 1.7

感谢您的帮助!

编辑:这是我的build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="BRANCH_CONTROLLER" default="do-deploy" basedir="."
    xmlns:fx="javafx:com.sun.javafx.tools.ant">
    <target name="init-fx-tasks">
        <path id="fxant">
            <filelist>
                <file name="${java.home}\..\lib\ant-javafx.jar" />
                <file name="${java.home}\lib\jfxrt.jar" />
                <file name="${basedir}" />
            </filelist>
        </path>

        <taskdef resource="com/sun/javafx/tools/ant/antlib.xml" uri="javafx:com.sun.javafx.tools.ant"
            classpathref="fxant" />
    </target>
    <target name="setup-staging-area">
        <delete dir="externalLibs" />
        <delete dir="project" />
        <delete dir="projectRefs" />
        <delete dir="externalResources" />

        <mkdir dir="externalLibs" />

        <copy todir="externalLibs">
            <fileset
                dir="D:\Travail\Programmation\alphabetiseur_caligula\AlphabetiseurLib">
                <filename name="alphabetiseurlib.jar" />
            </fileset>
        </copy>
        <copy todir="externalLibs">
            <fileset
                dir="D:\Travail\Programmation\alphabetiseur_caligula\BRANCH_CONTROLLER\libs">
                <filename name="jfxrt.jar" />
            </fileset>
        </copy>
        <copy todir="externalLibs">
            <fileset
                dir="D:\Travail\Programmation\alphabetiseur_caligula\BRANCH_CONTROLLER\libs">
                <filename name="ddmlib.jar" />
            </fileset>
        </copy>

        <mkdir dir="externalResources" />

        <copy todir="externalResources">
            <fileset
                dir="D:\Travail\Programmation\alphabetiseur_caligula\BRANCH_CONTROLLER\resources\platform-tools">
            </fileset>
        </copy>

        <copy todir="externalResources">
            <fileset
                dir="D:\Travail\Programmation\alphabetiseur_caligula\BRANCH_CONTROLLER\resources">
                <filename name="Alphabetiseur.apk" />
            </fileset>
        </copy>

        <mkdir dir="project" />
        <copy todir="project">
            <fileset
                dir="D:\Travail\Programmation\alphabetiseur_caligula\BRANCH_CONTROLLER">
                <include name="src/**" />
            </fileset>
        </copy>

        <mkdir dir="projectRefs" />
        <copy todir="projectRefs">
            <fileset dir="D:\Travail\Programmation\alphabetiseur_caligula">
                <include name="AlphabetiseurLib\src/**" />
            </fileset>
        </copy>
    </target>
    <target name='do-compile'>
        <delete dir="build" />
        <mkdir dir="build/src" />
        <mkdir dir="build/libs" />
        <mkdir dir="build/classes" />
        <mkdir dir="build/resources" />

        <!-- Copy project-libs references -->
        <copy todir="build/libs">
            <fileset dir="externalLibs">
                <include name="jfxrt.jar" />
                <include name="ddmlib.jar" />
                <include name="alphabetiseurlib.jar" />
            </fileset>
        </copy>

        <!-- Copy project resources references -->
        <copy todir="build/externalResources">
            <fileset dir="externalResources">
                <include name="*" />
            </fileset>
        </copy>

        <!-- Copy project references -->
        <copy todir="build/src">
            <fileset dir="projectRefs/AlphabetiseurLib/src">
                <include name="**/*" />
            </fileset>
        </copy>

        <!-- Copy project sources itself -->
        <copy todir="build/src">
            <fileset dir="project/src">
                <include name="**/*" />
            </fileset>
        </copy>

        <javac includeantruntime="false" source="1.7" target="1.7"
            srcdir="build/src" destdir="build/classes" encoding="UTF-8">
            <classpath>
                <fileset dir="build/libs">
                    <include name="*" />
                </fileset>
                <filelist>
                    <file
                        name="D:\Travail\Programmation\alphabetiseur_caligula\java\lib\jfxrt.jar" />
                </filelist>
            </classpath>
        </javac>

        <!-- Copy over none Java-Files -->
        <copy todir="build/classes">
            <fileset dir="project/src">
                <exclude name="**/*.java" />
            </fileset>
        </copy>

        <copy todir="build/classes">
            <fileset dir="projectRefs/AlphabetiseurLib/src">
                <exclude name="**/*.java" />
            </fileset>
        </copy>

    </target>
    <target name="do-deploy" depends="setup-staging-area, do-compile, init-fx-tasks">
        <delete file="dist" />
        <delete file="deploy" />

        <mkdir dir="dist" />
        <mkdir dir="dist/libs" />
        <mkdir dir="dist/resources"/>

        <copy todir="dist/libs">
            <fileset dir="externalLibs">
                <include name="*" />
            </fileset>
        </copy>

        <copy todir="dist/resources">
            <fileset dir="externalResources">
                <include name="*" />
            </fileset>
        </copy>


        <fx:resources id="appRes">
            <fx:fileset dir="dist" includes="BRANCH_CONTROLLER.jar" />
            <fx:fileset dir="dist" includes="libs/*" />
            <fx:fileset dir="dist" includes="resources/**" />
        </fx:resources>

        <fx:application id="fxApplication" name="AlphabetiseurControlleur"
            mainClass="application.AlphabetiseurControl" toolkit="fx" />

        <mkdir dir="build/classes/META-INF" />



        <fx:jar destfile="dist/BRANCH_CONTROLLER.jar">
            <fx:application refid="fxApplication" />
            <fileset dir="build/classes">
            </fileset>
            <fx:resources refid="appRes" />

            <manifest>
                <attribute name="Implementation-Vendor" value="Alexandre Jubert" />
                <attribute name="Implementation-Title" value="AlphabetiseurControlleur" />
                <attribute name="Implementation-Version" value="1.0" />
                <attribute name="JavaFX-Feature-Proxy" value="None" />
            </manifest>
        </fx:jar>


        <mkdir dir="deploy" />
        <!-- Need to use ${basedir} because somehow the ant task is calculating 
            the directory differently -->
        <fx:deploy embedJNLP="true" extension="false" includeDT="false"
            offlineAllowed="true" outdir="${basedir}/deploy" outfile="BRANCH_CONTROLLER"
            nativeBundles="exe" updatemode="background">

            <fx:platform basedir="${java.home}" />
            <fx:info title="Alphabetiseur Controlleur" vendor="alex" />

            <fx:application refId="fxApplication" />
            <fx:resources refid="appRes" />
            <fx:preferences install="true" shortcut="true"/>
        </fx:deploy>


    </target>
</project>

编辑09/02:我可以显示对话框,但System.getProperty("user.dir")会返回empty string。无法理解为什么。

0 个答案:

没有答案