创建具有“下载并运行”经验的Java11 JavaFX应用程序的正确方法是什么?

问题描述

我使用JDK11和JavaFX创建了Java / JavaFx应用程序。它可以在我的intellij IDE系统上正常运行。

每当我尝试将其打包为可安装的EXE,MSI或什至从命令行将其作为胖子运行时,都会遇到各种错误。通常,它抱怨没有包含东西。

一些特定的错误消息:

胖jar,通过控制台调用jar:> java -cp MLSNUM_to_PUBREC-0.8.7-SNAPSHOT-jar-with-dependencies.jar com.company.Main 错误:缺少JavaFX运行时组件,并且是运行此应用程序所必需的

jpackager:(多个Alert_error窗口)未能找到库/bin/msvcr100.dll,无法找到jni_createJavavm,无法启动JVM

我已经多次创建了一个胖子罐,我确信自己做了正确制作胖子罐的过程。我已经尝试了javapackager,jpackager,jpackage,izpack等。我认为我的项目结构总体上存在缺陷,并且会导致错误

我的问题: 构建Java11 / JavaFX应用程序的正确方法是什么?它需要com.gargoylesoft的HTMLUnit和JavaFX吗?

这是我的代码

package com.company;

import com.gargoylesoftware.htmlunit.*;
import com.gargoylesoftware.htmlunit.html.*;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.FileChooser;
import org.apache.bcel.generic.RETURN;
import org.junit.After;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

import java.io.*;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javafx.application.Application;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;




public class Main extends Application{

static WebClient webClient;
static CookieManager cookieManager;
private static String username = "User";
private static String password = "Pass";
static File InputFile;
private static String downloadPubRec;
private static String formattedStringMLS;
private static ArrayList<String> downloadMLSNums;
private static String downloadFinal;
private static ArrayList<String> publicRecordArrayListFinal = new ArrayList<String>();


public void start(Stage stage) throws Exception {
    Label l = new Label("MLS2PubRec");
    Scene scene = new Scene(new StackPane(l),640,480);
    stage.setTitle("MLS2PubRec");
    FileChooser fileChooser = new FileChooser();
    GridPane grid = new GridPane();
    grid.setAlignment(Pos.CENTER);
    grid.setHgap(10);
    grid.setVgap(10);
    grid.setPadding(new Insets(25,25,25));

    Scene scene2 = new Scene(grid,500,450);
    Text scenetitle = new Text("Welcome!");
    scenetitle.setFont(Font.font("Tahoma",FontWeight.norMAL,20));
    grid.add(scenetitle,2,1);

    Label userName = new Label("Pinergy User Name:");
    grid.add(userName,1);

    TextField userTextFieldInputFX = new TextField();
    grid.add(userTextFieldInputFX,1,1);

    Label pw = new Label("Pinergy Password:");
    grid.add(pw,2);

    PasswordField pwBoxInputFX = new PasswordField();
    grid.add(pwBoxInputFX,2);

    Button btnStartApplication = new Button("Download Public Records");
    HBox hbBtn = new HBox(10);
    hbBtn.setAlignment(Pos.BottOM_RIGHT);
    hbBtn.getChildren().add(btnStartApplication);
    grid.add(hbBtn,4);
    final Text actiontarget = new Text();
    grid.add(actiontarget,6);
    actiontarget.setId("actiontarget");

    Button btnFileChoose = new Button("Select MLS# File");
    btnFileChoose.setonAction(e -> {
        File selectedFile = fileChooser.showOpenDialog(stage);
        InputFile = selectedFile;
    });

    HBox hbBtn2 = new HBox(12);
    hbBtn2.setAlignment(Pos.BottOM_CENTER);
    hbBtn2.getChildren().add(btnFileChoose);
    grid.add(hbBtn2,4);


    btnStartApplication.setonAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent e) {
                actiontarget.setFill(Color.FIREBRICK);
                username = userTextFieldInputFX.getText();
                password = pwBoxInputFX.getText();

                try {
                    if(InputFile == null){actiontarget.setText("File not found"); }
                    else{
                        actiontarget.setText("");
                        init(); Date date= new Date();
                        Format formatter = new SimpleDateFormat("YYYY-MM-dd_hh-mm-ss");
                            if(download(InputFile)){ actiontarget.setText("File downloaded with name:         
MLSNum_to_PubRec" + formatter.format(date) +".csv");

                        close(); }
                            else{actiontarget.setText("Error"); close();}
                    }
                } catch (Exception ex) {
                    ex.printstacktrace();
                }
            }
        });


        stage.setScene(scene2);
        stage.show();
    }





public void init() throws Exception {

    webClient = new WebClient(browserVersion.CHROME);
    cookieManager = new CookieManager();
    cookieManager = webClient.getCookieManager();
    cookieManager.setCookiesEnabled(true);
    webClient.getoptions().setJavaScriptEnabled(true);
    webClient.getoptions().setThrowExceptionOnScriptError(false);
    webClient.getoptions().setCssEnabled(false);
    webClient.getoptions().setRedirectEnabled(true);


}



public static HtmlPage signIn() throws Exception {
    //Acquire location for URI,password,username,submitbutton


    HtmlPage page1 = webClient.getPage("http://h3c.mlspin.com/signin.asp");


    HtmlButton cookieButton = page1.getFirstByXPath("//*    
[@id=\"cookieConsentBootstrapModal\"]/div/div/div[3]/button");
    page1 = cookieButton.click();

    HtmlForm form = page1.getFormByName("loginform");
    HtmlTextInput uName = form.getInputByName("user_name");
    HtmlPasswordInput passWord = form.getInputByName("pass");
    HtmlButton button = form.getFirstByXPath("//*[@id=\"loginForm\"]/table/tbody/tr[7]/td/button");


    uName.setValueAttribute(username);
    passWord.setValueAttribute(password);
    HtmlPage page2 = button.click();
    Thread.sleep(2200);
    //If login error,login
    if (page2.getUrl().toString().contains("Login_Error")){
        System.out.println("Login error,reattempting (User already signed in)");
        HtmlButton loginErrorButton = 
page2.getFirstByXPath("/html/body/div/table/tbody/tr[2]/td/center/button");
        page2 = loginErrorButton.click();
    }
    System.out.println("Getpage2 as text: " + page2.getUrl().toString());
    System.out.println("Results p2 URI  " + page2.getPage());
    System.out.println("Results p1 URI   " + page1.getPage());


    if (page2.getUrl().toString().contains("signin.asp")) {
        return null;
    }
    else{return page2;}
}
//Does minor formating,downloads file
public static boolean download(File InputFile) throws Exception {
    if (InputFile == null) {
        return false;
    } else {
        HtmlPage page3 = signIn();
        HtmlAnchor toolLinkBtn = page3.getFirstByXPath("/html/body/header/section[2]/nav/a[7]");
        page3 = toolLinkBtn.click();
        HtmlAnchor publicRecordLinkBtn =page3.getFirstByXPath(""/html/body/table/tbody/tr[2]/td[2]");
        page3 = publicRecordLinkBtn.click();


        List<List<String>> records = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new FileReader(InputFile))) {
            String line;
            int iter = 0;
            while ((line = br.readLine()) != null) {
                String[] values = line.split(",");
                records.add(Arrays.asList(values));
                formattedStringMLS = Arrays.asList(values).toString()
                        .replace(",","")  //remove the commas
                        .replace("[","")  //remove the right bracket
                        .replace("]","")  //remove the left bracket
                        .trim();           //remove trailing spaces from partially initialized arrays
                System.out.println("MLSNUM Added  : " + formattedStringMLS + "");
                page3 = (HtmlPage) page3.refresh();

                HtmlInput inputMLSNumberBox = page3.getElementByName("List_No");
                HtmlElement searchNowBtn = page3.getElementByName("searchNowimg");


                inputMLSNumberBox.setValueAttribute(formattedStringMLS);

                page3 = searchNowBtn.click();


                Thread.sleep(200);
                if ((page3.getVisibleText().contains("No Property Matches Found") == false) && (page3.getVisibleText().contains("Error Processing MLS #") == false)) {
                    HtmlElement selectAllCheckBox = page3.getElementByName("selall");
                    HtmlAnchor downloadBtn = page3.getAnchorByHref("javascript:DownloadResults('0');");
                    page3 = selectAllCheckBox.click();

                    System.out.println(iter);
                    String downloadTemp = (downloadBtn.click().getWebResponse().getContentAsstring());
                     downloadTemp = downloadTemp.replaceAll("[\\n\\r\\t]+","");
                    downloadTemp = downloadTemp.replaceAll("Estimated Tax","Estimated Tax"+ "\r" +"\"" + formattedStringMLS + "\",");



                    downloadPubRec = (downloadPubRec + downloadTemp);





                }
            }
            String tempContent = downloadPubRec.replaceAll("Owner_Address","OwnerAddTemp");
            tempContent = tempContent.replaceAll("null","");
            tempContent = tempContent.replaceAll("Address,MLSNumber,Address,");
            tempContent = tempContent.replaceAll("OwnerAddTemp","Owner_Address");

          downloadFinal = tempContent;
            System.out.println("Testing Length "+ downloadFinal);
            Date date = new Date();
            Format formatter = new SimpleDateFormat("YYYY-MM-dd_HH-mm-ss");

            System.out.println("Done at " + date.toString());
            FileWriter writer = new FileWriter(new File("MLSNum_to_PubRec" + formatter.format(date) + ".csv"));
            writer.write(downloadFinal);
            alertDownloadFinished();
            writer.flush();


        }
        return true;
    }
}


@After
public static void close() throws Exception {


    webClient.close();
    cookieManager.clearCookies();
    Thread.sleep(200);
    System.exit(0);
}



private static void alertDownloadFinished() {
        Date date = new Date();
        Format formatter = new SimpleDateFormat("YYYY-MM-dd_hh-mm-ss");
        Alert downloaDalert = new Alert(Alert.AlertType.@R_885_4045@ION,"");  //new alert object
        downloaDalert.setTitle("MLS2PubRec: Public Records Downloaded!");  //warning Box title
        downloaDalert.setHeaderText("Output Filename: MLSNum_to_PubRec" + formatter.format(date) + ".csv");// Header
         //Description of warning
        downloaDalert.getDialogPane().setPrefSize(400,100); //sets size of alert Box

        downloaDalert.showAndWait();


}
}

我的POM文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

<groupId>groupId</groupId>
<artifactId>MLSNUM_to_PUBREC</artifactId>
<version>0.8.8-SNAPSHOT</version>
<build>
        <plugins>

                    <plugin>
                        <artifactId>maven-assembly-plugin</artifactId>
                        <configuration>
                            <archive>
                                <manifest>
                                    <mainClass>com.company.Main</mainClass>
                                </manifest>
                            </archive>
                            <descriptorRefs>
                                    <descriptorRef>jar-with- 
   dependencies</descriptorRef>
                            </descriptorRefs>
                        </configuration>
                        <executions>
                            <execution>
                                <id>make-assembly</id> <!-- this is used 
for 
inheritance merges -->
                                <phase>package</phase> <!-- bind to the 
packaging phase -->
                                <goals>
                                    <goal>single</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>com.company.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

        <plugin>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-maven-plugin</artifactId>
            <version>0.0.4</version>
            <configuration>
                <mainClass>com.company.Main</mainClass>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>11</source>
                <target>11</target>
            </configuration>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-controls</artifactId>
        <version>14</version>
    </dependency>
    <dependency>
        <groupId>net.sourceforge.htmlunit</groupId>
        <artifactId>htmlunit</artifactId>
        <version>2.43.0</version>
    </dependency>
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-launcher</artifactId>
        <version>1.0.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.2.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.2.0</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.2.0</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13</version>
        <scope>compile</scope>
    </dependency>


</dependencies>
<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)