Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 代码工作正常,然后在maven安装之后,jar和IDE中都会出现错误_Java_Eclipse_Maven_Fxml - Fatal编程技术网

Java 代码工作正常,然后在maven安装之后,jar和IDE中都会出现错误

Java 代码工作正常,然后在maven安装之后,jar和IDE中都会出现错误,java,eclipse,maven,fxml,Java,Eclipse,Maven,Fxml,应用程序启动方法中出现异常: Exception in Application start method java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.

应用程序启动方法中出现异常:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at edu.citytech.view.EmployeeMain.start(EmployeeMain.java:16)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    ... 1 more
Exception running application edu.citytech.view.EmployeeMain
下面是使用主方法EmployeeMain.java的类:

package edu.citytech.view;

import edu.citytech.utitlity.GlobalVariables;
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;

public class EmployeeMain extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {

        Parent root = FXMLLoader.load(getClass().getResource("EmployeeView.fxml")); 

        primaryStage.setScene(new Scene(root));
        primaryStage.show();

        // * new *
        primaryStage.setResizable(false);   
        primaryStage.setTitle("Employee List");

        primaryStage.getIcons().add(new Image(EmployeeMain.class.getResourceAsStream("People.jpg")));
    }

    public static void main(String[] args) {    

        if (args.length > 0) {
            GlobalVariables.portNumber = (Integer.parseInt(args[0]));           
        }       

        launch(args);
    }
}
解决这个问题的方法是向任何文件添加一个空间并保存它,然后重新启动EclipseIDE,并从IDE再次运行它。当我运行mvn clean和mvn install,然后运行它创建的jar文件时,会破坏它。我们班上每个人都有这个问题,老师不知道如何解决。它只对某些人有效,对其他人无效。然而这一次,无论我怎么尝试,这都不起作用。在我使用maven创建jar之前,它只能在IDE中工作。直到我修改并保存一个文件,然后重新启动IDE,它才会工作。所有东西都正确命名,它只是工作,而不是随机工作。这是IDE问题还是项目问题

我该怎么做才能使jar文件在运行时工作?

3问题

  • 听起来您需要IDE重新编译代码,以便它也能在eclipse中工作?不知道为什么会这样,除非它使用的是java版本
  • eclipse通常会在给定任何更改(例如,添加到文件中的空间)的情况下重建

    EclipseIDE和maven编译是否使用相同的java版本

    删除您的./target目录并

    将此添加到maven pom.xml以确保您使用的是正确的java版本(例如java 1.8):

    
    1.8
    1.8
    
  • 在jar构建中包含该文件
  • 此外,您的EmployeeView.fxml副本应位于src/main/resources中,并且该文件不会进入您的jar文件,除非您包括:

                 <resources>
                    <resource>
                        <directory>src/main/resources</directory>
                        <includes>
                            <include>**/*</include>
                        </includes>
                    </resource>
                </resources>
    
    
    src/main/resources
    **/*
    
  • 该代码在运行时无法在jar文件中工作请参见以下答案:
  • 并更新代码以使用getREsourceAsStream,例如,我使用以下方法:

    public void load(String resource, HashSet<String> hashset) {
            ClassLoader classloader = Thread.currentThread().getContextClassLoader();
            InputStream is_awl = classloader.getResourceAsStream(resource);
    
            BufferedReader reader = new BufferedReader(new InputStreamReader(is_awl));
            String line;
            try {
                while ((line = reader.readLine()) != null) {
                    hashset.add(line.trim());
                }
    
            } catch (IOException e) {
                System.err.println("LexicalFeatures can't initialise:"
                        + resource + " hashset: "+ e.getMessage());
                e.printStackTrace();
            }
    
        }
    
    公共无效加载(字符串资源,哈希集){
    ClassLoader ClassLoader=Thread.currentThread().getContextClassLoader();
    InputStream是_awl=classloader.getResourceAsStream(资源);
    BufferedReader reader=新的BufferedReader(新的InputStreamReader(is_awl));
    弦线;
    试一试{
    而((line=reader.readLine())!=null){
    add(line.trim());
    }
    }捕获(IOE异常){
    System.err.println(“词典功能无法初始化:”
    +resource+“hashset:+e.getMessage());
    e、 printStackTrace();
    }
    }
    
    我找到了让它工作的方法。很烦人,但很简单。我将在这里为任何可能通过搜索引擎找到此页面的人编写

  • 右键单击项目文件夹>Maven>更新项目

  • 更新所选项目

  • 右键单击项目文件夹并在本地终端中运行

  • mvn安装


  • 并且不要进行MVN清理

    检查
    EmployeeView.fxml
    是否包含在点击非常接近的
    jar
    中。它实际上不在罐子里。除非我将其复制到src/main/resources。但是它仍然不起作用,我认为它必须与主方法所在的包在同一个包中。
    public void load(String resource, HashSet<String> hashset) {
            ClassLoader classloader = Thread.currentThread().getContextClassLoader();
            InputStream is_awl = classloader.getResourceAsStream(resource);
    
            BufferedReader reader = new BufferedReader(new InputStreamReader(is_awl));
            String line;
            try {
                while ((line = reader.readLine()) != null) {
                    hashset.add(line.trim());
                }
    
            } catch (IOException e) {
                System.err.println("LexicalFeatures can't initialise:"
                        + resource + " hashset: "+ e.getMessage());
                e.printStackTrace();
            }
    
        }