0

Ive built a very simple notifications system for my application, this is the full class:

public class Notification extends JFrame {
    Timer timer;
    private static int count = 0;
    private String from;
    private String msg;
    private String time;
    private final JLabel jLabel1;
    private final JLabel jLabel2;
    private final JLabel jLabel3;
    private final JLabel jLabel4;    

    public void NotificationStart(int seconds) {
        timer = new Timer();
        timer.schedule(new RemindTask(), seconds*1000);
    }

    class RemindTask extends TimerTask {
        public void run() {
            System.out.format("Remove notification");             
            timer.cancel(); //Terminate the timer thread

            dispose(); // Remove the window

            count--; 
        }
    }    

    public Notification(String from, String msg, String time) {
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();

        getContentPane().setLayout(null);
        setSize(308,77);
        setBackground(new Color(0, 255, 0, 0));        
        setLocationRelativeTo(null);
        setUndecorated(true); 
        setAlwaysOnTop(true);

        jLabel2.setFont(new java.awt.Font("Lucida Grande", 1, 12)); 
        jLabel2.setText(from + ":");
        jLabel2.setBounds(38, 11, 240, 15);
        jLabel1.add(jLabel2);

        jLabel3.setFont(new java.awt.Font("Lucida Grande", 0, 12)); 
        jLabel3.setText(msg);
        jLabel3.setBounds(38, 25, 240, 50);
        jLabel1.add(jLabel3);

        jLabel4.setBounds(280, 6, 16, 16);
        jLabel1.add(jLabel4);        

        // Start timer
        NotificationStart(8);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/notification.png")));
        getContentPane().add(jLabel1);
        jLabel1.setBounds(0, 0, 308, 77);            

        // Position it
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice defaultScreen = ge.getDefaultScreenDevice();
        Rectangle rect = defaultScreen.getDefaultConfiguration().getBounds();
        int x = (int) rect.getMaxX() - (this.getWidth() + 10);
        int y;

        if(this.count == 0) {
            y = (int) rect.getMinY() - 46 + this.getHeight();
        } else {
            y = (int) rect.getMinY() + 30 + (this.getHeight() * this.count);
        }


        this.setLocation(x, y); 
        this.setVisible(true);
        this.count = count + 1;

        jLabel1.addMouseListener(new MouseAdapter() {  
            public void mouseReleased(MouseEvent e) {   
                // Remove notification if user clicks it
                dispose();
            }

            public void mouseEntered(MouseEvent e) {   
                // Show the close icon
                jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/icnClose.png")));
            }            

            public void mouseExited(MouseEvent e) {   
                // Hide the close icon
                jLabel4.setIcon(null);
            }            

        }); 
    }

To spawn a notification i use the following code in my main JFrame:

new Notification("Thomas", "New notification!", "13.25");

Everything works exactly like the way i want it to, as long as i run the code within NetBeans. If i do a Clean and Build from NetBeans and try to run the executable jar the application just stops.

I know that the error is somehow in this class, cause if im not calling the Notification class anywhere from my main JFrame the jar executes just fine.

Any ideas?

Alosyius
  • 8,771
  • 26
  • 76
  • 120
  • any exception that gets printed? – rajesh Jul 24 '13 at 11:29
  • Cant find any info what so ever :// – Alosyius Jul 24 '13 at 11:30
  • You said its an executable jar. can you add a test log as the first line in your main() and see if its getting printed – rajesh Jul 24 '13 at 11:31
  • Everything before the `new Notification("Thomas", "New notification!", "13.25");` gets printed – Alosyius Jul 24 '13 at 11:35
  • I have a manifest, buts its the auto generated on from NetBeans only contains `Manifest-Version: 1.0 X-COMMENT: Main-Class will be added automatically by build` – Alosyius Jul 24 '13 at 11:36
  • Main class needs to be added. Also see ans from Mike – rajesh Jul 24 '13 at 11:37
  • Added the Main class no difference – Alosyius Jul 24 '13 at 11:40
  • execute you program from a console and if you are not hiding the exception you would see the stacktrace and hece the cause of the problem. If you did swing visual editing you would need to add those jars to the classpath of your jar – fGo Jul 24 '13 at 11:42
  • UPDATE: I got a exception now `java.awt.IllegalComponentStateException: The frame is decorated` – Alosyius Jul 24 '13 at 11:43
  • http://stackoverflow.com/questions/7353799/is-it-possible-to-have-a-translucent-windows-in-java-7-including-a-title-bar and http://docs.oracle.com/javase/7/docs/api/java/awt/Frame.html#setUndecorated%28boolean%29 – fGo Jul 24 '13 at 11:47

1 Answers1

0

It's probably a dependency issue when you are trying to run the executable jar. There are a number of maven (assuming you're using maven with Netbeans since that's the normal use case these days) plugins for bundling the dependency jars with the executable jar.

Try this from the command line instead of java -jar to help you debug:

mvn exec:java -Dexec.mainClass="com.example.Main" [-Dexec.args="argument1"] ...

Mike Thomsen
  • 36,828
  • 10
  • 60
  • 83