简体   繁体   English

为什么我的 JAR 文件没有运行? (IntelliJ, JavaFX, Maven)

[英]Why does my JAR file not run? (IntelliJ, JavaFX, Maven)

I've created a Java/JavaFX application in intelliJ.我在 intelliJ 中创建了一个 Java/JavaFX 应用程序。 I created it as a Java application and later imported neccesary parts for JavaFX.我将其创建为 Java 应用程序,然后为 JavaFX 导入了必要的部件。

I've tried a variety of installer/packager creators from Izpack, Javapackager, Jpackager, Jpackage, and creating a fat jar. Nothing will work for me.我已经尝试了来自 Izpack、Javapackager、Jpackager、Jpackage 的各种安装程序/打包程序创建程序,并创建了一个胖子 jar。没有什么对我有用。

The application runs just fine in IntelliJ.该应用程序在 IntelliJ 中运行良好。

Why won't it run elsewhere?为什么它不会在其他地方运行?

Here is my POM, Code, and Error messages.这是我的 POM、代码和错误消息。

I'd pay 10$ if you could fix this issue.如果你能解决这个问题,我愿意支付 10 美元。

Some specific error messages:一些具体的错误信息:

Fat Jar, calling jar via console: > java -cp MLSNUM_to_PUBREC-0.8.7-SNAPSHOT-jar-with-dependencies.jar com.company.Main Error: JavaFX runtime components are missing, and are required to run this application Fat Jar, calling jar via console: > java -cp MLSNUM_to_PUBREC-0.8.7-SNAPSHOT-jar-with-dependencies.jar com.company.Main Error: JavaFX runtime components are missing, and are required to run this application

jpackager: (Multiple alert_error windows) Failed to find library /bin/msvcr100.dll failed to locate jni_createJavavm, failed to launch JVM jpackager: (Multiple alert_error windows) Failed to find library /bin/msvcr100.dll failed to locate jni_createJavavm, failed to launch JVM

Here is my code:这是我的代码:

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, 25));

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

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

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

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

    PasswordField pwBoxInputFX = new PasswordField();
    grid.add(pwBoxInputFX, 1, 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, 1, 4);
    final Text actiontarget = new Text();
    grid.add(actiontarget, 1, 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, 0, 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.INFORMATION, "");  //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();


}
}

My POM file:我的 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>

@Reizo's comment pointed me in the correct direction. @Reizo 的评论为我指明了正确的方向。

I recreated my Java app as a JavaFX app (IntelliJ's prebuilt settings)我将我的 Java 应用程序重新创建为 JavaFX 应用程序(IntelliJ 的预构建设置)

Changed my overall project structure, moving to having a main method that wasnt inside the application extension.改变了我的整体项目结构,转向拥有一个不在应用程序扩展中的主要方法。

As well I updated all my java versions to be Java 14.我也将我所有的 java 版本更新为 Java 14。

Consider this issue closed.考虑关闭此问题。 Thank you kindly @Reizo.谢谢@Reizo。 I really appreciate the advice and offer for assistance.我非常感谢您的建议和提供的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM