[英]how to create a executable jar file which can execute the methods from other java classes in same package
文件夹结构的图像:我的项目的文件夹结构
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它没有执行其他类的方法。 所以有人可以帮我创建一个 jar 文件。
我按照本网站https://www.java67.com/2014/04/how-to-make-executable-jar-file-in-Java-Eclipse.ZFC336988883A2中的步骤创建了 jar 文件
当用户单击“生成”按钮时,它将开始执行同一个 class 中的方法,并且在该方法中我正在调用另一个 class 方法。
方法
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";
}
}
生成按钮执行代码
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);
}
}
}
});
我将总结解决方案。 依赖库未打包在 JAR 中。 因此,导致
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.
一个快速的解决方法是通过 IDE 复制生成的 jar。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.