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

Satish

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);
                }
            }
        }
    });
Prateek Pande

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to convert a .java or a .jar file into a Linux executable file ( without a .jar extension, which means it's not a .jar file )

From Java

How can I get elements by tag from a span class which has the same name as other classes?

From Java

Java - How to find out which Jar file an import comes from?

From Java

Can I execute Two different Classes from same jar file?

From Java

Java Executable file from JAR

From Java

how to know which class / package/ methods used by given jar file?

From Java

Java execute jar which depends on other jar from command line

From Java

How to create an executable jar which is executable (Maven-Jfoenix-Hibernate-IntelliJ) __ Can someone makes the subject resolve/close please

From

Not able to download Zip file from gmail which contains executable Jar in it

From Dev

How to create an executable file (or JAR) from a running program?

From Dev

How to create an executable Java file?

From Dev

How to create executable jar file from springboot - maven application?

From Dev

How can I make a .jar file executable?

From Dev

How can I execute a .jar file from the terminal

From Dev

How to create Windows executable from JAR

From Dev

How to execute a jar executable file and receive the return value

From Dev

How can I create a HashMap that dynamically calls other Java classes?

From Dev

cannot execute binary file - Executable Jar file

From Dev

Java: how to open one Jframe from another Jframe which is not in the same file but in the same package;

From Dev

How can I invoke a method or access a field from other class in the same package in JAVA

From Dev

How to execute executable which added to PATH from powershell?

From Dev

How can I directly access methods from my library class in other classes? Java

From Dev

How to execute the jar file from a java class

From Dev

Can I execute an executable file from a webview?

From Dev

How to see Java executable jar file now running and stop jar file from GUI or notification window?

From Dev

How can I create an executable file from this source code?

From Dev

How to create an executable link to jar file?

From Dev

How to create jar file without java classes but with a folder using maven

From Dev

How do I make a class that can create a HTML element with an onclick attribute which can call methods from the same class?

Related Related

  1. 1

    How to convert a .java or a .jar file into a Linux executable file ( without a .jar extension, which means it's not a .jar file )

  2. 2

    How can I get elements by tag from a span class which has the same name as other classes?

  3. 3

    Java - How to find out which Jar file an import comes from?

  4. 4

    Can I execute Two different Classes from same jar file?

  5. 5

    Java Executable file from JAR

  6. 6

    how to know which class / package/ methods used by given jar file?

  7. 7

    Java execute jar which depends on other jar from command line

  8. 8

    How to create an executable jar which is executable (Maven-Jfoenix-Hibernate-IntelliJ) __ Can someone makes the subject resolve/close please

  9. 9

    Not able to download Zip file from gmail which contains executable Jar in it

  10. 10

    How to create an executable file (or JAR) from a running program?

  11. 11

    How to create an executable Java file?

  12. 12

    How to create executable jar file from springboot - maven application?

  13. 13

    How can I make a .jar file executable?

  14. 14

    How can I execute a .jar file from the terminal

  15. 15

    How to create Windows executable from JAR

  16. 16

    How to execute a jar executable file and receive the return value

  17. 17

    How can I create a HashMap that dynamically calls other Java classes?

  18. 18

    cannot execute binary file - Executable Jar file

  19. 19

    Java: how to open one Jframe from another Jframe which is not in the same file but in the same package;

  20. 20

    How can I invoke a method or access a field from other class in the same package in JAVA

  21. 21

    How to execute executable which added to PATH from powershell?

  22. 22

    How can I directly access methods from my library class in other classes? Java

  23. 23

    How to execute the jar file from a java class

  24. 24

    Can I execute an executable file from a webview?

  25. 25

    How to see Java executable jar file now running and stop jar file from GUI or notification window?

  26. 26

    How can I create an executable file from this source code?

  27. 27

    How to create an executable link to jar file?

  28. 28

    How to create jar file without java classes but with a folder using maven

  29. 29

    How do I make a class that can create a HTML element with an onclick attribute which can call methods from the same class?

HotTag

Archive