当前位置: 首页 > 知识库问答 >
问题:

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

郭琦
2023-03-14

我在intelliJ中创建了一个Java /JavaFX应用程序。我创建了它作为一个Java的应用程序,后来为JavaFX导入了必要的部件。

我尝试了Izpack、Javapackager、Jpackager、Jpackage和创建胖jar中的各种安装程序/打包程序创建者。我什么都不行。

该应用程序在IntelliJ中运行良好。

为什么它不会在别处运行?

这是我的POM、代码和错误消息。

如果你能解决这个问题,我会付10美元。

一些特定的错误消息:

Fat Jar,通过控制台调用Jar:

JPackager:(多个alert_error窗口)找不到库 /bin/msvcr100.dll找不到jni_createJavavm,启动JVM失败

这是我的密码:

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();


}
}

我的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>

共有1个答案

万俟渊
2023-03-14

@Reizo的评论为我指明了正确的方向。

我将Java应用程序重新创建为JavaFX应用程序(IntelliJ的预构建设置)

改变了我的整体项目结构,转而使用不在应用程序扩展中的主方法。

我还将所有java版本更新为Java14。

考虑关闭此问题。谢谢你亲切@Reizo!我真的很感谢你的建议和帮助。

 类似资料:
  • 每次运行导出的文件(该文件包含一个以图像为图标的

  • 问题内容: 我正在尝试在Linux计算机上使用Eclipse Kepler运行一些JavaFX代码,并安装e(fx)clipse插件: 我的理解是,尽管自JDK 7u6版本起,JavaFX就已包含在标准JDK中,但有意将JavaFX运行时JAR文件jfxrt.jar留在了Java运行时路径上,直到在JavaFX与其余Java基础结构之间进行进一步测试为止完成了。因此,在创建新的Java项目时,必须

  • 我一直试图将一个maven项目打包到一个jar文件中,但它无法运行,因为javafx似乎有一个问题,下面是我的构建路径、项目结构(使用JavaFX14)和依赖关系 我尝试了两种方式来运行,第一种方式是使用提供的vm参数运行我的主应用程序文件。在ide中工作很好,但当试图作为jar文件运行时,会出现以下错误... 下一件我厌倦的事情是创建一个独立的主并从它运行,就像这样 有什么想法吗?

  • 我使用Netbeans和JavaFx Scene Builder构建了一个JavaFx项目。 在我完成复制粘贴之前,一切工作都很顺利。 这些文件是: 以下是文件: