简体   繁体   English

如何创建一个可执行的 jar 文件,该文件可以执行同一 package 中其他 java 类的方法

[英]how to create a executable jar file which can execute the methods from other java classes in same package

Image of folder structure: Folder Structure of my project文件夹结构的图像:我的项目的文件夹结构

I am trying to create a executable jar file from the eclipse.. I have 3 classes in java package, in that one class is main class and other 2 classes contains some methods which are there in the main class. I am trying to create a executable jar file from the eclipse.. I have 3 classes in java package, in that one class is main class and other 2 classes contains some methods which are there in the main class. I have checked in the online and created a jar file but it is not executing the output from the other class methods.. how I know is I ran the same in eclipse and it is giving output but when I running it from the executable jar file it is not executing the methods of other classes. I have checked in the online and created a jar file but it is not executing the output from the other class methods.. how I know is I ran the same in eclipse and it is giving output but when I running it from the executable jar file它没有执行其他类的方法。 so can some one please help me to create a jar file.所以有人可以帮我创建一个 jar 文件。

I have created a jar file by following the steps in this site https://www.java67.com/2014/04/how-to-make-executable-jar-file-in-Java-Eclipse.html我按照本网站https://www.java67.com/2014/04/how-to-make-executable-jar-file-in-Java-Eclipse.ZFC336988883A2中的步骤创建了 jar 文件

UI Image用户界面图片

when user click on the Generate button it will start the execution of method which is in the same class, and inside that method I'm calling the other class method.当用户单击“生成”按钮时,它将开始执行同一个 class 中的方法,并且在该方法中我正在调用另一个 class 方法。

Method方法

public static String generateOutput(String url, String tagName) {
        XpathUITest output = new XpathUITest();

        try {
            return output.xpathBuilder(url, tagName);
        } catch (IOException | InterruptedException e) {
            return "Failed with exceotion, please try again";
        }
    }

Generate button execution code生成按钮执行代码

bj.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                
                JLabel j = null;
                url = userInput.getText();
                if(!url.startsWith("http") || !url.startsWith("https")){
                    url="";
                }
                locType = combo.getSelectedItem().toString();
                tagName = comboTags.getSelectedItem().toString();
                boolean executeOutput = false;
                JPanel output = new JPanel(new BorderLayout(4, 4));
                output.setPreferredSize(new DimensionUIResource(500, 350));
                JPanel resOpJP = new JPanel(new GridLayout(0, 1, 4, 4));
                resOpJP.add(new JLabel("Output : ", SwingConstants.LEFT));
                JButton downLoad = new JButton("Download in file");

                JPanel Message = new JPanel();
                if (url.isEmpty()
                        && (selectedFile != null && selectedFile.endsWith(".html") && !selectedFile.isEmpty())) {
                    j = new JLabel("Selected File/URL is " + selectedFile, SwingConstants.RIGHT);
                    inputPath = selectedFile;
                    executeOutput = true;
                } else if ((url.startsWith("http") || url.startsWith("https"))
                        && (selectedFile == null || selectedFile.isEmpty())) {
                    j = new JLabel("Selected File/URL is " + url, SwingConstants.RIGHT);
                    inputPath = url;
                    executeOutput = true;
                } else if ((url==null ||url.isEmpty()) && (selectedFile == null || selectedFile.isEmpty())) {
                    JOptionPane.showMessageDialog(Message, "Please provide a valid html file / provide a URL", "Error",
                            JOptionPane.ERROR_MESSAGE);
                } else if (selectedFile != null && !selectedFile.isEmpty()) {
                    if (selectedFile.contains("html") && !(new File(selectedFile).exists())) {
                        JOptionPane.showMessageDialog(Message, "Please provide a valid html file path", "Error",
                                JOptionPane.ERROR_MESSAGE);
                        selectedFile = "";
                    } else if (!selectedFile.endsWith(".html") && (new File(selectedFile).exists())) {
                        JOptionPane.showMessageDialog(Message, "Please provide a valid html file", "Error",
                                JOptionPane.ERROR_MESSAGE);
                        selectedFile = "";
                    }
                } else if (!url.isEmpty() && (!url.startsWith("http") || !url.startsWith("https"))) {
                    JOptionPane.showMessageDialog(Message, "Please provide a valid URL", "Error",
                            JOptionPane.ERROR_MESSAGE);
                }

                if (locType.contains("Select Locator") || tagName.contains("Select Tag")) {
                    executeOutput = false;
                    JOptionPane.showMessageDialog(Message, "Locator Type and Tag is mandatory", "Error",
                            JOptionPane.ERROR_MESSAGE);
                } else if (!locType.contains("Select Locator") && !tagName.contains("Select Tag")) {
                    executeOutput = true;
                }
                if (executeOutput) {

    ///*****This is the step where other class method invoke***///
                    xpathUI.expectedOutput = generateOutput(inputPath, tagName);
                    if (xpathUI.expectedOutput.equals("No html tag found in the provided html")) {
                        JOptionPane.showMessageDialog(new JPanel(), xpathUI.expectedOutput, "Information",
                                JOptionPane.WARNING_MESSAGE);
                    } else if(xpathUI.expectedOutput.contains("Error")){
                        JOptionPane.showMessageDialog(new JPanel(), xpathUI.expectedOutput, "Error Message",
                                JOptionPane.ERROR_MESSAGE);
                    }else {
                    downLoad.addActionListener(new ActionListener() {

                        @SuppressWarnings("null")
                        @Override
                        public void actionPerformed(ActionEvent e) {
                                JFrame jF = new JFrame();
                                jF.setSize(300, 300);
                                choose = new JFileChooser();
                                choose.showSaveDialog(jF);
                                choose.setCurrentDirectory(new File(System.getProperty("user.home")));
                                choose.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                                try {
                                    String saveFile = "";
                                    try {
                                        saveFile = choose.getSelectedFile().getAbsolutePath();
                                        System.out.println(saveFile);
                                    } catch (Exception ex) {
                                        saveFile = "";
                                    }

                                    if ((saveFile != null || !saveFile.isEmpty())
                                            && !saveFile.split(".txt")[0].isEmpty()) {
                                        FileOutputStream fout = new FileOutputStream(new File(saveFile));
                                        fout.write(xpathUI.expectedOutput.getBytes());
                                        fout.flush();
                                        fout.close();
                                        if (new File(saveFile).exists()) {
                                            JOptionPane.showMessageDialog(new JPanel(),
                                                    "Locator file saved successfully at :\n" + saveFile,
                                                    "Confirmation Message", JOptionPane.INFORMATION_MESSAGE);
                                        }
                                    }

                                } catch (IOException e1) {
                                    e1.printStackTrace();
                                }
                            }
                    });

                    j.setFont(new FontUIResource("Arial", Font.PLAIN, 10));
                    resOpJP.add(j);
                    output.add(resOpJP, BorderLayout.LINE_START);
                    JPanel text = new JPanel(new GridLayout(0, 1));
                    JTextArea jta = new JTextArea();
                    jta.setText("");
                    jta.setText(xpathUI.expectedOutput);
                    jta.setLineWrap(true);
                    jta.setRows(15);
                    jta.setColumns(1);
                    text.add(jta);
                    output.add(text, BorderLayout.PAGE_END);
                    JOptionPane.showOptionDialog(null, output, "XPATH Generator : Output", JOptionPane.DEFAULT_OPTION,
                            JOptionPane.INFORMATION_MESSAGE, null, new Object[] { downLoad }, null);
                }
            }
        }
    });

I'll summarize the solution.我将总结解决方案。 Dependent libraries were not getting packaged in the JAR.依赖库未打包在 JAR 中。 Thus, resulting in因此,导致

NoClassDefFoundError:org/jsoup/Jsoup

Thus, repackaging the jsoup dependency using maven https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html solved this issue. Thus, repackaging the jsoup dependency using maven https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html solved this issue.

A quick workaround was to copy the generated jar by the IDE.一个快速的解决方法是通过 IDE 复制生成的 jar。

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

相关问题 我可以从同一个jar文件中执行两个不同的类吗? - Can I execute Two different Classes from same jar file? 如何创建可以分发或打包的可执行jar文件? - How to create an executable jar file which can be distributed or packaged? 我如何从我的java .jar文件创建可执行的apple .app文件? - How can i create executable apple .app file from my java .jar file? java -cp可以报告在可执行类中导入的每个包中找到的jar或类文件吗? - Can `java -cp` report the jar or class file which it finds for each package imported in an executable class? Java执行jar,它依赖于命令行中的其他jar - Java execute jar which depends on other jar from command line 如何制作一个包含Java和jar文件的可执行jar - how to make an executable jar which includes a java and a jar file 包中的类如何在同一包的静态方法中使用其他类? - How can a class in a package use other classes in the same package's static methods? 如何从终端创建可执行jar文件 - How to create an executable jar file from terminal 如何从包中的每个.java文件创建JAR文件 - How to create JAR-file from every .java file in package 如何创建动态jar文件以从入口点(主)获取多个类及其方法的访问而无需导入Java? - How to create dynamic jar file to get access of multiple classes and its methods from an entry point(main) without importing in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM