Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
183 views
in Technique[技术] by (71.8m points)

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 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. so can some one please help me to create a jar file.

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

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.

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 :
" + 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);
                }
            }
        }
    });
question from:https://stackoverflow.com/questions/65642673/how-to-create-a-executable-jar-file-which-can-execute-the-methods-from-other-jav

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I'll summarize the solution. Dependent libraries were not getting packaged in the 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.

A quick workaround was to copy the generated jar by the IDE.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...