调用方法时出错。并且无法启动 JVM - 本机包将构建但不会启动

Error invoking method. and Failed to launch JVM - Native Package will build but not launch

任何人都可以更具体地说明我的问题所在以及如何解决它吗?

我是运行宁:

对于 Marco 在 code.makery.ch (code.makery.ch/library/javafx-8-tutorial/part7/)

上的教程,我已经仔细并反复地按照说明进行操作

我之前部署过这个程序的一个较早的本地包,遇到了一些麻烦,但最终在添加

之后成功了
-vm
C:\Program Files\Java\jdk1.8.0_91\bin\javaw.exe

到eclipse.ini

在最终将 ant build 成功 运行 之后,

do-deploy:
        [copy] Copying 2 files to C:\Users\administrator.SUNDANCE\IdeaProjects\POA 1.1 Build Master\POA 1.1 - Try 1\build\dist\libs
       [mkdir] Created dir: C:\Users\administrator.SUNDANCE\IdeaProjects\POA 1.1 Build Master\POA 1.1 - Try 1\build\build\classes\META-INF
Using base JDK at: C:\Program Files\Java\jdk1.8.0_91\jre
Using base JDK at: C:\Program Files\Java\jdk1.8.0_91\jre
Installer (.exe) saved to: C:\Users\administrator.SUNDANCE\IdeaProjects\POA 1.1 Build Master\POA 1.1 - Try 1\build\deploy\bundles
BUILD SUCCESSFUL
Total time: 56 seconds

我的主文件如下所示:

 import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;

/**
 * Created by Brad on 5/20/2016.
 * Solely used to load the FXML and set the icons. Everything else is done in Controller.java
 */
public class Main extends Application {
    public static FXMLLoader loader;

    //Icon from https://icons8.com

    @Override
    public void start(Stage primaryStage) throws Exception{
        loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("view/Arrivals_Layout1.fxml"));
        Parent root = loader.load();
        primaryStage.getIcons().add(new Image(ClassLoader.getSystemResourceAsStream("resources/images/Pallet-96.png")));
        primaryStage.getIcons().add(new Image(ClassLoader.getSystemResourceAsStream("resources/images/pallet_96_allsizes.ico")));



        primaryStage.setTitle("Purchase Order Arrivals");
        primaryStage.setScene(new Scene(root));
        primaryStage.setMinWidth(820);
        primaryStage.setMinHeight(375);
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

我的 Package Explorer 看起来像 this,如果有帮助的话 我的路径:C:\ProgramData\Oracle\Java\javapath;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\IBM\Client Access\Emulator;C:\Program Files (x86)\IBM\Client Access\Shared;C:\Program Files (x86)\IBM\Client Access\;C:\Program Files\Microsoft SQL Server0\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server0\Tools\Binn\ManagementStudio\;C:\Program Files (x86)\Microsoft SQL Server0\Tools\Binn\;C:\Program Files\Microsoft SQL Server0\Tools\Binn\;C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\PrivateAssemblies\;C:\Program Files (x86)\Microsoft SQL Server0\DTS\Binn\;C:\Program Files\TortoiseHg\;C:\Program Files (x86)\Inno Setup 5

build.xml:

<?xml version="1.0" encoding="UTF-8"?>
    <project name="POA 1.1 - Try 1" 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" />

        <mkdir dir="externalLibs" />

        <copy todir="externalLibs">
            <fileset dir="C:\Program Files\Java\sqljdbc_4.2\enu">
                <filename name="sqljdbc42.jar"/>
            </fileset>
        </copy>
        <copy todir="externalLibs">
            <fileset dir="C:\Users\administrator.SUNDANCE\IdeaProjects\POA 1.1 Build Master\POA 1.1 - Try 1\build\dist">
                <filename name="POA 1.1 - Try 1.jar"/>
            </fileset>
        </copy>

        <mkdir dir="project" />
        <copy todir="project">
            <fileset dir="C:\Users\administrator.SUNDANCE\IdeaProjects\POA 1.1 Build Master\POA 1.1 - Try 1">
                <include name="src/**" />
            </fileset>
        </copy>

        <mkdir dir="projectRefs" />
    </target>
    <target name='do-compile'>
        <delete dir="build" />
        <mkdir dir="build/src" />
        <mkdir dir="build/libs" />
        <mkdir dir="build/classes" />

        <!-- Copy project-libs references -->
        <copy todir="build/libs">
            <fileset dir="externalLibs">
                <include name="sqljdbc42.jar"/>
                <include name="POA 1.1 - Try 1.jar"/>
            </fileset>
        </copy>

        <!-- Copy project references -->

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

        <javac includeantruntime="false" source="1.8" target="1.8" srcdir="build/src" destdir="build/classes" encoding="Cp1252">
            <classpath>
                <fileset dir="build/libs">
                    <include name="*"/>
                </fileset>
            </classpath>
        </javac>

        <!-- Copy over none Java-Files -->
        <copy todir="build/classes">
        <fileset dir="project/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" />

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


        <fx:resources id="appRes">
            <fx:fileset dir="dist" includes="POA 1.1 - Try 1.jar"/>
            <fx:fileset dir="dist" includes="libs/*"/>
            <fx:fileset dir="dist" includes="resources/**"/>
        </fx:resources>

        <fx:application id="fxApplication"
            name="Purchase Order Arrivals"
            mainClass="Main"
            version="1.1"
        />

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



        <fx:jar destfile="dist/POA 1.1 - Try 1.jar">
            <fx:application refid="fxApplication"/>
            <fileset dir="build/classes">
            </fileset>
            <fx:resources refid="appRes"/>

            <manifest>
                <attribute name="Implementation-Vendor" value="Ugma Development"/>
                <attribute name="Implementation-Title" value="Purchase Order Arrivals"/>
                <attribute name="Implementation-Version" value="1.1"/>
                <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="false"
            extension="false"
            includeDT="false"
            offlineAllowed="true"
            outdir="${basedir}/deploy"
            outfile="POA 1.1 - Try 1" nativeBundles="exe"
            updatemode="background" >

            <fx:platform basedir="${java.home}"/>
            <fx:info title="POA 1.1 - Try 1" vendor="Ugma Development"/>

            <fx:application refId="fxApplication"/>
            <fx:resources refid="appRes"/>
        </fx:deploy>


    </target>
</project>

生成 .exe 并将程序安装在 /AppData 中。

该程序将在 Eclipse 中编译并且 运行 正常,但是当我 运行 安装的版本时,我得到:

Error invoking method.

首先,然后在单击“确定”之后

Failed to launch JVM

我似乎什么都试过了:

我看了很多类似的问题,也搜索了互联网,但还没有解决这个问题。我读过的许多答案含糊不清或没有答案的例子:

因为自己部署不太熟练,Marco自己说要来问问Stack Exchange的浩浩荡荡的世界: http://code.makery.ch/library/javafx-8-tutorial/part7/#comment-2233862311

再次强调一下,我的问题出在哪里,以及如何解决?

我明白了!

我发现问题的方式:

经过双重、三次检查依赖项并看到它编译和构建都很好,我怀疑位于 build/deploy/{yourProjectName}.jar

的 jar 文件

编辑: 如果仅在安装后出现错误,那么 运行 安装目录中的 jar 是有意义的。 ( AppData/Local/{ApplicationTitle}/app/{yourProjectName}.jar )

我运行在命令行看它是否抛出异常:

为了轻松导航到我在资源管理器中保存我的项目时记下的目录,然后将其复制到命令行。

  1. 打开命令提示符

    Win + r

    cmd + 输入

  2. 导航到目录

    cd {ProjectPath}\build\deploy

  3. 运行 jar 文件

    java -jar "{YourJar}.jar"

因为我是通过命令提示符执行 jar,java 有地方可以向我显示异常!

C:\Users\administrator.SUNDANCE\IdeaProjects\PODTester_Layout8\build\deploy>java
 -jar "PODTester_Layout8.jar"
java.io.FileNotFoundException: src\resources\Carrier List.txt (The system cannot
 find the path specified)
        at java.io.FileInputStream.open0(Native Method)
        at java.io.FileInputStream.open(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at java.io.FileReader.<init>(Unknown Source)

检查我的代码后,我发现它没有找到该文件,因为它明确引用了 src。我修好了,你瞧!重建后,安装的应用程序运行正常!


总而言之:

如果你的原生包抛出一个

Error invoking method.

还有一个

Failed to launch JVM

运行 你的 jar 文件通过命令提示符查看它是否抛出异常。


现在,如果您想详细了解我的具体问题以及我解决问题的过程,请点击此处:

问题:

我认为这可能与依赖关系有关(尤其是对我而言,包括 sqljdbc 驱动程序),但事实证明这不是依赖关系问题。问题是 .jar 仅在 安装后 . 引发异常"Could not invoke method." 这给了我很少的新信息。

为了减轻未来类似的问题,我添加了一个 showExceptionDialog() 方法(受 Marco Jacob's work 启发):

/**
 * Shows a dialog box when an exception occurs instead of just doing nothing.
 *
 * Once installed this will help to diagnose problems instead of letting
 * them go unnoticed.
 *
 * @param e the exception to print; it's stacktrace will be shown as well
 */
public static void showExceptionDialog(Exception e) {
    Alert alert = new Alert(Alert.AlertType.ERROR);

    alert.setTitle("Exception Dialog");
    alert.setHeaderText("An error occurred:");

    String content = "Error: ";
    if (null != e) {
        content += e.toString() + "\n\n";
    }

    alert.setContentText(content);

    Exception ex = new Exception(e);

    //Create expandable Exception.
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    ex.printStackTrace(pw);

    String exceptionText = sw.toString();

    //Set up TextArea
    TextArea textArea = new TextArea(exceptionText);
    textArea.setEditable(false);
    textArea.setWrapText(true);


    textArea.setPrefHeight(600);
    textArea.setPrefWidth(800);


    //Set expandable Exception into the dialog pane.
    alert.getDialogPane().setExpandableContent(textArea);


    alert.showAndWait();
}

关于我的问题的具体细节:

在我的程序中,我有一个名为 Carrier List.txt 的文件正在读取和写入,它存储在 resources 文件夹中。

在 IntelliJ 和 Eclipse 中它编译得很好,因为它可以找到 src/resources/Carrier List.txt 很好。当我按照 Marco Jacob's Deployment Tutorial 手动复制资源文件夹时,一切都会很好,但我明确引用了

src/resources/Carrier List.txt

在我的代码中,而不仅仅是

resources/Carrier List.txt