-1

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 :\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);
                }
            }
        }
    });
Satish
  • 3
  • 3
  • Hi and welcome. Can you share your code? How did you run the executable jar? Show example of how you executed it and what the output was from that as well as from Eclipse. – DarkMatter Jan 09 '21 at 12:39
  • Are you getting some error related to Main() not found. Is your main() getting called and you are getting a ClassNotFoundException for the other 2 classes. If issue with main(), try this - java -cp MyJar.jar com.mycomp.myproj.ClassWithMainMethod – Prateek Pande Jan 09 '21 at 12:48
  • @DarkMatter, i have updated my question with image and code... for your convenience, i have added a comment in code where that method will start execute... – Satish Jan 09 '21 at 12:50
  • @PrateekPande, as I'm new to this, I don't know how to see the logs of the jar execution..can you help me to get the jar execution logs... – Satish Jan 09 '21 at 12:51
  • Yeah, finally when i ran it from cmd.. i got the execution log and it is not getting the dependencies for the class.. it requires JSoup, and execution is failing with the `NoClassDefFoundError:org/jsoup/Jsoup` – Satish Jan 09 '21 at 12:56
  • That means while packaging the jar, dependencies were not included. – Prateek Pande Jan 09 '21 at 12:57
  • If you are using maven, refer https://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven – Prateek Pande Jan 09 '21 at 12:58
  • Yes.. so how can i get that jar package... i have my dependencies in pom.xml so can i add dependencies manually to build path and try or how can I go on this? – Satish Jan 09 '21 at 12:58
  • Check this https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html – Prateek Pande Jan 09 '21 at 13:02
  • If you just want the jar, copy the jar from the output folder after executing the project from your IDE. – Prateek Pande Jan 09 '21 at 13:03
  • Thanks Prateek.. I will try that and will upvote your comment if that works for me.. – Satish Jan 09 '21 at 13:04
  • @PrateekPande, Hi Prateek, Your solution worked for me.. Thank you. – Satish Jan 09 '21 at 13:24

1 Answers1

0

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.

Prateek Pande
  • 495
  • 3
  • 12