1

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. 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.

I have the manifest.txt, which was generated by Eclipse, located in the META-INF file folder.

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:

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?

Thanks in advance for any help!

I ran the cmd java -jar my.jar and got

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. It runs fine in the IDE though.

Danny
  • 21
  • 2
  • 8
  • Can you post the contents of your manifest file? It should have something in there about a `Main-Class`. – austin Aug 20 '13 at 15:45
  • 1) Run it from the command line using `java -jar the.jar` and copy/paste the output. 2) For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Aug 20 '13 at 15:52
  • Did you export as a Runnable Jar File? You need to do that. First create a run configuration (Project -> Run As -> Run Configurations) where you set the main class, then Export -> Java -> Runnable JAR File and select the desired run configuration. If you believe you've done that, include a sample of your manifest. – user1676075 Aug 20 '13 at 16:02
  • Or else you can try to create `JAR` File [manually](http://stackoverflow.com/a/15187181/1057230) – nIcE cOw Aug 20 '13 at 16:21
  • The contents of the manifest file are: Manifest-Version: 1.0 Main-Class: binaparts.main.RunProgram the So there is an empty new line after the Main-Class line – Danny Aug 20 '13 at 17:10
  • is there a main method in that wall of code? – Gilbert Le Blanc Aug 20 '13 at 17:14
  • I did change the name of the Main class to RunProgram. @user1676075 I did check the run configuration and it is just as you stated. – Danny Aug 20 '13 at 17:18
  • @GilbertLeBlanc There is not a main method in the GUI section of code. Only in the main class to run the program – Danny Aug 20 '13 at 17:33
  • Do you have this config.properties file in the folder on the disk, or in the folder in the jar file only? Your error message indicates that the program is looking for the properties file in the file system; if you only have it in the jar file, then it won't find it. You can use Java resources to load the properties file (see Class.getResource() and related tutorials), though you can't store values there that are going to change. – arcy Aug 20 '13 at 17:42
  • @rcook The config.properies file is in the jar only. I was hoping to keep everything for the application located in just the one file. Is there a standard place that a file like that would normally be kept? – Danny Aug 20 '13 at 17:55

2 Answers2

2

Again: "you can use Java resources to load the properties file as an (see the tag info for related tutorials), though you can't store values there that are going to change". 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.

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.

Community
  • 1
  • 1
arcy
  • 12,845
  • 12
  • 58
  • 103
  • @trashgod Even using the properties settings the way they are now with my program, a config.properties file is created in the same location that the jar file is located when the jar is double clicked. The GUI will still do nothing at all. Shouldn't I at least be getting the GUI to display even if clicking one of the buttons would cause it to error? – Danny Aug 21 '13 at 12:30
  • "... when the jar is double clicked." - until you get things running, I recommend you stick with a command line startup, in order to have a command window to display errors and stacktraces. Whether the GUI will display depends on how the code is written; if, for instance, (JUST ONE example), you are attempting to get a value from configProperties and then attempt to dereference it, that would give a null pointer and crash. Error messages are the best way to get info on why something isn't working, and we can't help you much on SO without them. – arcy Aug 21 '13 at 14:36
  • @Danny: Just guessing, you're opening a `File`, when you should be using `getResourceAsStream()`. – trashgod Aug 21 '13 at 15:59
  • @trashgod: actually we already covered that; click the "see more comments" button for the comments to the original post. We've established that the OP has a file, which explains why it works in the development environment but not in the jar. He seems to have other questions now. – arcy Aug 21 '13 at 17:09
  • For my original question, I found out that the file path that was calling the images for creation of panels was "/Images/picture.jpg" on some of the calls when it should have been "/images/picture.jpg" which was causing the GUI to not open for the program at all outside of the IDE, but work fine inside of the IDE. – Danny Aug 21 '13 at 17:23
  • Well, if this answered your question, or one of your questions, please mark it as an "answer"; it's part of what makes the the SO world turn 'round. – arcy Aug 21 '13 at 17:32
  • rcook: I see; thanks. Danny: The EDT will restart itself after an exception, but an inconsistent state may leave the screen blank. – trashgod Aug 21 '13 at 19:22
0

Another tip: use

java.awt.*; //to import everything from java.awt
javax.swing.*; //to import everything from javax.swing
Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
Alex
  • 19
  • 2