无法在 JAR 中使用外部库部署 JavaFX 应用程序 [英] Unable to deploy JavaFX applicaiton with external libraries in a JAR

查看:26
本文介绍了无法在 JAR 中使用外部库部署 JavaFX 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Eclipse 中开发 JavaFX 应用程序.该应用程序具有外部库,如 log4j 和其他库,并且可以从 Eclipse 完美运行.我尝试通过 Eclipse 内置功能将它从 Eclipse 部署为 Runnable jar,它在某些计算机上运行良好,而在其他计算机上它会给我一个模糊的未定义链接错误.经过仔细研究,似乎需要通过 ANT 脚本或通过 javafxpackager 应用程序对 JavaFX 进行特殊打包.

I'm developing a JavaFX application in Eclipse. The app has external libraries, like log4j and others and runs perfectly from Eclipse. I've tried deploying it from Eclipse as a Runnable jar through the Eclipse built-in feature, and it runs well on some computers, while on others it would give me an obscure Undefined Link error. After digging around it seems JavaFX needs to be specially packaged either through an ANT script or through the javafxpackager application.

在我的一生中,我似乎无法通过这两种方法中的任何一种来部署可运行的 jar.尝试这两种方法中的任何一种,我都会得到一个异常.我尝试过的事情:

For the life of me I can't seem to deploy a runnable jar through either of those methods. Trying either of those methods, I get an Exception. Things I've tried:

  1. 通过 ANT 脚本构建 JavaFX 应用程序,以下是构建脚本相关部分的片段

  1. Build the JavaFX application through an ANT script, below is a snippet of the relevant parts of the build script

<path id="classpath">
    <fileset dir="${lib.dir}" includes="**/*.jar"/>
    <fileset dir="${javafx.sdk.path}/jre/lib" includes="jfxrt.jar"/>
</path>

<target name="compile" depends="clean">
    <echo>Compiling the source</echo>  
    <mkdir dir="${classes.dir}"/>
    <!-- Copy over the misc files into the classes dir -->
    <copy todir="${classes.dir}/bundles">
       <fileset dir="${src.dir}/bundles"/>
    </copy>
    <copy todir="${classes.dir}/css">
        <fileset dir="${src.dir}/css"/>
    </copy>
    <copy todir="${classes.dir}/img">
       <fileset dir="${src.dir}/img"/>
    </copy>
    <copy todir="${classes.dir}/views">
       <fileset dir="${src.dir}/views"/>
    </copy>

    <copy todir="${classes.dir}/bundles">
       <fileset dir="${src.dir}/bundles"/>
    </copy>

    <copy file="${src.dir}/log4j2.xml" todir="${classes.dir}"/>

    <javac target="1.7" srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath" debug="on">
    </javac>
</target>

<target name="jar" depends="compile">
    <echo>Creating the main jar file</echo>  
    <mkdir dir="${distro.dir}" />
    <fx:jar destfile="${distro.dir}/main.jar" verbose="true">
        <fx:platform javafx="2.1+" j2se="7.0"/>
        <fx:application mainClass="${main.class}"/>

        <!-- What to include into result jar file?
             Everything in the build tree-->
        <fileset dir="${classes.dir}"/>

        <!-- Define what auxilary resources are needed
              These files will go into the manifest file,
              where the classpath is defined -->
         <fx:resources>
            <fx:fileset dir="${distro.dir}" includes="main.jar"/>
            <fx:fileset dir="." includes="${lib.dir}/**" type="jar"/>
            <fx:fileset dir="." includes="."/>
        </fx:resources>

        <!-- Make some updates to the Manifest file -->
        <manifest>
           <attribute name="Implementation-Vendor" value="${app.vendor}"/>
           <attribute name="Implementation-Title" value="${app.name}"/>
           <attribute name="Class-Path" value="${lib.dir}"/> 
           <attribute name="Implementation-Version" value="1.0"/>
        </manifest>
    </fx:jar>
</target>

通过 ant 脚本编译/生成 jar,我在控制台中弹出一个窗口半秒和应用程序启动方法中的异常",没有堆栈跟踪.我已经尝试使用 -XX:-OmitStackTraceInFastThrow 标志运行 java,但它仍然不会给我堆栈跟踪.

Compiling/generating the jar through the ant script, I get a window pop up for half a second and "Exception in Application start method" in the console, with no stack trace. I've tried runnning java with the -XX:-OmitStackTraceInFastThrow flag, but it still won't give me a stack trace.

  1. 通过 javafxpackager 运行会出现以下错误:

  1. Running through javafxpackager gives the following error:

RenderJob.run:内部异常java.lang.UnsatisfiedLinkError: com.sun.prism.d3d.D3DContext.nSetBlendEnabled(JZZ)我在 com.sun.prism.d3d.D3DContext.nSetBlendEnabled(本机方法)在 com.sun.prism.d3d.D3DContext.initState(D3DContext.java:84)在 com.sun.prism.d3d.D3DResourceFactory.(D3DResourceFactory.java:57)在 com.sun.prism.d3d.D3DPipeline.createResourceFactory(D3DPipeline.java:147)在 com.sun.prism.d3d.D3DPipeline.getD3DResourceFactory(D3DPipeline.java:153)在 com.sun.prism.d3d.D3DPipeline.findDefaultResourceFactory(D3DPipeline.爪哇:179)在 com.sun.prism.d3d.D3DPipeline.getDefaultResourceFactory(D3DPipeline.java:201)在 com.sun.prism.GraphicsPipeline.getDefaultResourceFactory(GraphicsPipe行.java:97)在 com.sun.javafx.tk.quantum.QuantumRenderer$3.run(QuantumRenderer.java:143)在 java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)在 java.util.concurrent.FutureTask.runAndReset(FutureTask.java:304)在 com.sun.prism.render.RenderJob.run(RenderJob.java:37)在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.爪哇:1145)在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)在 com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.run(QuantumRenderer.java:98)在 java.lang.Thread.run(Thread.java:724)

RenderJob.run: internal exception java.lang.UnsatisfiedLinkError: com.sun.prism.d3d.D3DContext.nSetBlendEnabled(JZ Z)I at com.sun.prism.d3d.D3DContext.nSetBlendEnabled(Native Method) at com.sun.prism.d3d.D3DContext.initState(D3DContext.java:84) at com.sun.prism.d3d.D3DResourceFactory.(D3DResourceFactory.java:5 7) at com.sun.prism.d3d.D3DPipeline.createResourceFactory(D3DPipeline.java: 147) at com.sun.prism.d3d.D3DPipeline.getD3DResourceFactory(D3DPipeline.java: 153) at com.sun.prism.d3d.D3DPipeline.findDefaultResourceFactory(D3DPipeline. java:179) at com.sun.prism.d3d.D3DPipeline.getDefaultResourceFactory(D3DPipeline.j ava:201) at com.sun.prism.GraphicsPipeline.getDefaultResourceFactory(GraphicsPipe line.java:97) at com.sun.javafx.tk.quantum.QuantumRenderer$3.run(QuantumRenderer.java: 143) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:47 1) at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:304) at com.sun.prism.render.RenderJob.run(RenderJob.java:37) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor. java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor .java:615) at com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.run(Quantu mRenderer.java:98) at java.lang.Thread.run(Thread.java:724)

推荐答案

您可以使用 javafxpackager 部署您的应用程序.如果您部署一个自包含应用程序,Java 运行时环境将与您的应用程序捆绑在一起,并且无论是否安装了正确版本的 Java,它都可以在任何机器上运行.(你的 jdk 目录下的 bin 文件夹中应该有 javafxpackager.)

You can deploy your application using javafxpackager. If you deploy a self contained application, the Java Runtime Environment will be bundled with your application and it will run on any machine whether the right version of Java is installed or not. (You should have javafxpackager in your jdk directory under the bin folder.)

  1. 在 eclipse 工作区文件夹中查找已编译的文件 (.class).它们应该在 bin 目录中.

  1. Look for your compiled files (.class) in your eclipse workspace folder. They should be in the bin directory.

创建一个 createjar 文件夹,并在其中创建一个 classes 和一个 out 文件夹.复制 classes 文件夹中的所有 .class 文件.还将您的应用程序所需的所有 jar 文件包含在 classes 文件夹中.

Create a createjar folder and inside it create a classes and a out folder. Copy all of your .class files in the classes folder. Also include in the classes folder all jar needed by your application.

现在在命令行上进入 createjar 目录并运行以下命令:

Now on the command line go inside the createjar directory and run this command :

"C:\path\to\jdk\jdk1.7.0_25\bin\javafxpackager.exe" -createjar -appclass package.MainClass -srcdir classes -outdir out -outfile NameOfYourJar -classpath "" -v

"C:\path\to\jdk\jdk1.7.0_25\bin\javafxpackager.exe" -createjar -appclass package.MainClass -srcdir classes -outdir out -outfile NameOfYourJar -classpath "" -v

这应该会在 out 目录中创建一个可运行的 jar.下一步将需要它.

This should create a runnable jar in the out directory. You will need it for the next step.

  1. 现在在 createjar 文件夹旁边创建一个 deploy 文件夹.

  1. Now create a deploy folder alongside the createjar folder.

deploy 文件夹内制作其他目录distpackages.

Inside the deploy folder make to other directories dist and packages.

dist 文件夹中,从上面复制新创建的 jar 以及它需要的所有依赖项/资源.

In the dist folder copy your freshly created jar from above plus all the dependencies/ressources it will need.

返回命令行(如果您曾经离开过)并进入deploy 目录.从那里运行以下命令:

Go back on the command line (if you ever left it) and go in the deploy directory. Run the following command from there :

"C:\path\to\jdk\jdk1.7.0_25\bin\javafxpackager.exe" -deploy -native -outdir packages -outfile NameOfYourApp -srcdir dist -srcfiles NameOfYourJar.jar -appclass package.MainClass -name "Name of You Application" -title "Title of your application"

完成后它应该已经创建了部署应用程序所需的一切,包括在 Windows 平台上运行的本机 .exe 文件.这是自包含的应用程序!为了能够运行它,您需要进入 bundles\NameOfYourApp\app 并在此处粘贴您的应用需要的所有依赖项/资源.

Once it is done it should have created all you need to deploy your app including native .exe file to run on Windows platform. This is the self contained application ! To be able to run it you need to go into bundles\NameOfYourApp\app and paste here all dependencies/ressource your app needs.

最后,双击YourApp.exe,它甚至可以在没有安装Java的机器上运行.

At last, double click on YourApp.exe and it should run even on machines without Java installed.

这篇关于无法在 JAR 中使用外部库部署 JavaFX 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆