[英]Java Standalone runnable GUI application - can't get jar file to open the program
I am working on a GUI client that will be a standalone application to act as a front end to a MySQL database back end. 我正在使用一个GUI客户端,它将作为一个独立的应用程序充当MySQL数据库后端的前端。 I have all of the logic and coding done for the client but I can not for the life of me get the project to export to a runnable .jar file.
我已经为客户端完成了所有逻辑和编码,但是我无法终生将项目导出到可运行的.jar文件。
I have the manifest.txt, which was generated by Eclipse, located in the META-INF file folder. 我在META-INF文件夹中有一个由Eclipse生成的manifest.txt。
Here is my main method: 这是我的主要方法:
package binaparts.main;
import binaparts.gui.*;
public class Main{
public static void main(String[] args){
MainFrames m = new MainFrames();
m.displayGUI();
}
}
Here is the MainFrames class: 这是MainFrames类:
package binaparts.gui;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ItemListener;
import java.sql.*;
import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import org.json.JSONArray;
import org.json.JSONObject;
import binaparts.dao.*;
import binaparts.properties.ConfigurationManager;
public class MainFrames extends JFrame
{
private MainPanel main;
private CreatePanel create;
private UpdatePanel update;
private FindPanel find;
private SettingsPanel settings;
private ManageUsersPanel Manage;
JFrame frame = new JFrame("Main Menu:");
static final String configFilePath = "META-INF/config.properties";
DBConnect con = new DBConnect();
ConfigurationManager config = null;
public void run(){
displayGUI();
}
public void displayGUI()
{
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
contentPane.setLayout(new CardLayout());
main = new MainPanel(contentPane);
create = new CreatePanel(contentPane);
update = new UpdatePanel(contentPane);
find = new FindPanel(contentPane);
settings = new SettingsPanel(contentPane);
Manage = new ManageUsersPanel(contentPane);
contentPane.add(main, "Main Menu");
contentPane.add(create, "Create Part");
contentPane.add(update, "Update Part");
contentPane.add(find, "Find Part");
contentPane.add(settings, "Settings");
contentPane.add(Manage, "Manage Users");
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int height = screenSize.height;
int width = screenSize.width;
frame.setResizable(false);
frame.setSize(width/2, height/2);
frame.setLocationRelativeTo(null);
frame.setSize(700, 580);
frame.setContentPane(contentPane);
frame.setVisible(true);
}
class MainPanel extends JPanel{Contains code for that frame}
class MainPanel extends JPanel{Contains code for that frame}
class CreatePanel extends JPanel{Contains code for that frame}
class UpdatePanel extends JPanel{Contains code for that frame}
class FindPanel extends JPanel{Contains code for that frame}
class SettingsPanel extends JPanel{Contains code for that frame}
class ManageUsersPanel extends JPanel{Contains code for that frame}
}
I did not include the code for each panel so as not to clutter this. 我没有包括每个面板的代码,以免造成混乱。 There are also a couple other classes for managing configuration properties and database connections that work just fine.
还有另外两个类可以很好地管理配置属性和数据库连接。
My question is: Is there a problem with my main method/displayGUI interaction or with the process of creating the jar? 我的问题是:我的主要方法/ displayGUI交互或创建jar的过程是否存在问题?
Thanks in advance for any help! 在此先感谢您的帮助!
I ran the cmd java -jar my.jar and got 我运行了cmd java -jar my.jar并得到了
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\Danny>cd desktop
C:\Users\Danny\Desktop>cd executable
C:\Users\Danny\Desktop\Executable>java -jar BinaPartsManager.jar
java.io.FileNotFoundException: META-INF\config.properties (The system cannot fin
d the path specified)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at binaparts.properties.ConfigurationManager.save(ConfigurationManager.j
ava:41)
at binaparts.properties.ConfigurationManager.<init>(ConfigurationManager
.java:21)
at binaparts.dao.DBConnect.verifyUser(DBConnect.java:87)
at binaparts.gui.MainFrames$MainPanel.setStatus(MainFrames.java:110)
at binaparts.gui.MainFrames$MainPanel.<init>(MainFrames.java:137)
at binaparts.gui.MainFrames.displayGUI(MainFrames.java:66)
at binaparts.main.RunProgram.main(RunProgram.java:10)
java.lang.NullPointerException
at binaparts.dao.DBConnect.close(DBConnect.java:21)
at binaparts.gui.MainFrames$MainPanel.setStatus(MainFrames.java:127)
at binaparts.gui.MainFrames$MainPanel.<init>(MainFrames.java:137)
at binaparts.gui.MainFrames.displayGUI(MainFrames.java:66)
at binaparts.main.RunProgram.main(RunProgram.java:10)
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at binaparts.gui.MainFrames$MainPanel.<init>(MainFrames.java:147)
at binaparts.gui.MainFrames.displayGUI(MainFrames.java:66)
at binaparts.main.RunProgram.main(RunProgram.java:10)
I have the config.properties file stored in the META-INF folder. 我将config.properties文件存储在META-INF文件夹中。 It runs fine in the IDE though.
虽然它在IDE中运行良好。
Again: "you can use Java resources to load the properties file as an embedded-resource (see the tag info for related tutorials), though you can't store values there that are going to change". 再说一遍:“您可以使用Java资源将属性文件作为嵌入式资源加载(请参阅相关教程的标签信息 ),尽管您无法在其中存储要更改的值”。 You haven't said whether they're going to change.
您没有说他们是否会改变。
If you are going to store a few things, the (new-ish) way to do that is with the java.util.Preferences
API. 如果要存储一些东西,那么(新颖的)方法是使用
java.util.Preferences
API。
If you really need your own file, I suppose the user's' home directory is the place to put a directory of your own in which to store the file, look up user.home
as a system parameter. 如果您确实需要自己的文件,我假设用户的主目录是放置您自己的目录以存储文件的位置,请将
user.home
查找为系统参数。
Another tip: use 另一个提示:使用
java.awt.*; //to import everything from java.awt
javax.swing.*; //to import everything from javax.swing
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.