This discussion is archived
1 2 Previous Next 16 Replies Latest reply: Feb 23, 2010 2:31 AM by 793415 RSS

JAR file not executing fully

843804 Newbie
Currently Being Moderated
I use BlueJ to write my java applications. I created a .jar but it does not execute fully. I have a method at the start of main called showSplash() and at the end of main a new JFrame isset to open and the showSplash frame closes. This works fine when running it in BlueJ. However, after creating the jar and executing it the showSplash method runs but never closes and the second JFrame never displays. I know enough to see that the code is not reaching the second second myFrame.setVisible(true); function.

Any possible reasons why my code would execute fine within BlueJ but not from a .jar?

Thanks,
R
  • 1. Re: JAR file not executing fully
    793415 Pro
    Currently Being Moderated
    vt13 wrote:
    ...Any possible reasons why my code would execute fine within BlueJ but not from a .jar?
    Yes. To start finding out the reason in this case, make sure your code [prints a stacktrace in every catch statement|http://pscode.org/javafaq.html#stacktrace], then run it from the command line to see all output. If you cannot sort the problem from that, [copy/paste the exact error message|http://pscode.org/javafaq.html#exact] to the forum for further advice.
    Thanks,
    No worries.

    Edit 1:
    P.S. Welcome to the Sun forums.
  • 2. Re: JAR file not executing fully
    843804 Newbie
    Currently Being Moderated
    Thanks for the response.

    Is it a problem if i have no catch statements?

    R
  • 3. Re: JAR file not executing fully
    793415 Pro
    Currently Being Moderated
    vt13 wrote:
    ..Is it a problem if i have no catch statements?
    The 'problem' that might be introduced if the code has catch statements is that unless the code explicitly calls Exception.printStackTrace(), information will be lost. If the code does not have any catch statements, the default behaviour of the JRE is to printStackTrace().

    So no, it is not a problem if your code does not catch anything. (In fact, given that many people stuff up the printStackTrace() part, it is probably better that code does not catch anything that it does not absolutely have to.)

    Take the following 3 examples:
    1) Swallows the exceptions, ignoring them. VERY BAD.
    import java.io.*;
    import java.net.*;
    import javax.swing.*;
    
    class ExceptionHandling1 {
    
        public static void main(String[] args) {
            try {
                File f = new File("no-such-file.txt");
    
                URL url = f.toURI().toURL();
                JEditorPane jep = new JEditorPane();
                jep.setPage(url);
    
                JOptionPane.showMessageDialog(null, jep);
            } catch(MalformedURLException murle) {
                // completely ignore the Exception
            } catch(IOException ioe) {
                // completely ignore the Exception
            }
        }
    }
    2) Declare the method might throw exceptions.
    import java.io.*;
    import java.net.*;
    import javax.swing.*;
    
    class ExceptionHandling2 {
    
        public static void main(String[] args) throws MalformedURLException, IOException {
            File f = new File("no-such-file.txt");
    
            URL url = f.toURI().toURL();
            JEditorPane jep = new JEditorPane();
            jep.setPage(url);
    
            JOptionPane.showMessageDialog(null, jep);
        }
    }
    3) Catch the exceptions and print the stack trace.
    import java.io.*;
    import java.net.*;
    import javax.swing.*;
    
    class ExceptionHandling3 {
    
        public static void main(String[] args) {
            try {
                File f = new File("no-such-file.txt");
    
                URL url = f.toURI().toURL();
                JEditorPane jep = new JEditorPane();
                jep.setPage(url);
    
                JOptionPane.showMessageDialog(null, jep);
            } catch(MalformedURLException murle) {
                // print the stack trace to System.err
                murle.printStackTrace();
            } catch(IOException ioe) {
                // print the stack trace to System.err
                ioe.printStackTrace();
            }
        }
    }
    Output:
    andrew@pc1:/media/disk/projects/numbered/all/other$ java ExceptionHandling1
    andrew@pc1:/media/disk/projects/numbered/all/other$ java ExceptionHandling2
    Exception in thread "main" java.io.FileNotFoundException: /media/disk/projects/numbered/all/other/no-such-file.txt (No such file or directory)
         at java.io.FileInputStream.open(Native Method)
         at java.io.FileInputStream.<init>(FileInputStream.java:137)
         at java.io.FileInputStream.<init>(FileInputStream.java:96)
         at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:87)
         at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:178)
         at javax.swing.JEditorPane.getStream(JEditorPane.java:827)
         at javax.swing.JEditorPane.setPage(JEditorPane.java:433)
         at ExceptionHandling2.main(ExceptionHandling2.java:12)
    andrew@pc1:/media/disk/projects/numbered/all/other$ java ExceptionHandling3
    java.io.FileNotFoundException: /media/disk/projects/numbered/all/other/no-such-file.txt (No such file or directory)
         at java.io.FileInputStream.open(Native Method)
         at java.io.FileInputStream.<init>(FileInputStream.java:137)
         at java.io.FileInputStream.<init>(FileInputStream.java:96)
         at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:87)
         at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:178)
         at javax.swing.JEditorPane.getStream(JEditorPane.java:827)
         at javax.swing.JEditorPane.setPage(JEditorPane.java:433)
         at ExceptionHandling3.main(ExceptionHandling3.java:13)
    andrew@pc1:/media/disk/projects/numbered/all/other$ 
    As you can probably deduce. Forms 2 & 3 give us enough information to tell exactly what went wrong, whereas the first form not only fails, but provides no information about the failure.
  • 4. Re: JAR file not executing fully
    843804 Newbie
    Currently Being Moderated
    My main method and my showSplash method have no exception statements and neither declare that some may be thrown. Here is my code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.text.*;
    import java.util.*;
    import java.text.NumberFormat;
    import java.text.DecimalFormat;
    
    import javax.swing.JOptionPane;
    import javax.swing.JDialog;
    import javax.swing.JButton;
    import javax.swing.JRadioButton;
    import javax.swing.ButtonGroup;
    import javax.swing.JLabel;
    import javax.swing.ImageIcon;
    import javax.swing.BoxLayout;
    import javax.swing.Box;
    import javax.swing.BorderFactory;
    import javax.swing.border.Border;
    import javax.swing.JTabbedPane;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import java.beans.*; //Property change stuff
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.AbstractButton;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import javax.swing.ImageIcon;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.BorderFactory; 
    import javax.swing.border.Border;
    import javax.swing.border.TitledBorder;
    import javax.swing.border.EtchedBorder;
    import javax.swing.ImageIcon;
    import javax.swing.JTabbedPane;
    import javax.swing.JLabel;
    import javax.swing.JPanel; 
    import javax.swing.JFrame;
    import javax.swing.Box;
    import javax.swing.BoxLayout;
    
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.BorderLayout;
    
    import javax.swing.ImageIcon;
    
     
    import java.awt.Desktop;
    import java.awt.Image;
    import java.io.File;
    import java.io.IOException;
    import java.net.URI;
    import java.net.URISyntaxException;
    import java.net.URL;
    import javax.swing.ButtonModel;
    import javax.swing.ImageIcon;
    
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Image;
    
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    import java.net.*;
    
    public class Logon 
    {
    
    static JTextField userNameField ;
    static JPasswordField passwordField;
    static JFrame mainWindow;
    static JFrame mainFrame2;
    static JLabel message;
    static char defaultEcho;
    static int authError;
    static int authError2;
    static int currentCard = 1;
    static JPanel cardPanel;
    static CardLayout cl;
    static JPanel cardPanel2;
    static CardLayout cl2;
    static String previousUserName;
    
    
            private static final GridBagConstraints gbc;
        static {
            gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weightx = 1.0;
            gbc.weighty = 1.0;
            gbc.fill = GridBagConstraints.BOTH;
            gbc.anchor = GridBagConstraints.CENTER;
        }
        
        /**
         * Wraps a Swing JComponent in a background image. Simply invokes the overloded
         * variant with Top/Leading alignment for background image.
         *
         * @param component - to wrap in the a background image
         * @param backgroundIcon - the background image (Icon)
         * @return the wrapping JPanel
         */
        public static JPanel wrapInBackgroundImage(JComponent component,
                Icon backgroundIcon) {
            return wrapInBackgroundImage(
                    component,
                    backgroundIcon,
                    JLabel.CENTER,
                    JLabel.CENTER);
        }
        
        /**
         * Wraps a Swing JComponent in a background image. The vertical and horizontal
         * alignment of background image can be specified using the alignment
         * contants from JLabel.
         *
         * @param component - to wrap in the a background image
         * @param backgroundIcon - the background image (Icon)
         * @param verticalAlignment - vertical alignment. See contants in JLabel.
         * @param horizontalAlignment - horizontal alignment. See contants in JLabel.
         * @return the wrapping JPanel
         */
        public static JPanel wrapInBackgroundImage(JComponent component,
                Icon backgroundIcon,
                int verticalAlignment,
                int horizontalAlignment) {
            
            // make the passed in swing component transparent
            component.setOpaque(false);
            
            // create wrapper JPanel
            JPanel backgroundPanel = new JPanel(new GridBagLayout());
            
            // add the passed in swing component first to ensure that it is in front
            backgroundPanel.add(component, gbc);
            
            // create a label to paint the background image
            JLabel backgroundImage = new JLabel(backgroundIcon);
            
            // set minimum and preferred sizes so that the size of the image
            // does not affect the layout size
            backgroundImage.setPreferredSize(new Dimension(1,1));
            backgroundImage.setMinimumSize(new Dimension(1,1));
            
            // align the image as specified.
            backgroundImage.setVerticalAlignment(verticalAlignment);
            backgroundImage.setHorizontalAlignment(horizontalAlignment);
            
            // add the background label
            backgroundPanel.add(backgroundImage, gbc);
            
            // return the wrapper
            return backgroundPanel;
        }
    
        
    }
  • 5. Re: JAR file not executing fully
    843804 Newbie
    Currently Being Moderated
    and here is my main method. It wont look right because i took the styling out to try and make it fit on one post. No luck.
    public static void main(String[] args) 
        {
            showSplash();
            //ImageIcon img = new ImageIcon("1.bmp");
            
            //JWindow mainWindow = new JWindow();
            mainWindow = new JFrame("Portable Workstation");
            JPanel fieldPanel = new JPanel(new GridBagLayout());
            JPanel infoPanel = new JPanel(new BorderLayout());
            JPanel spacePanel = new JPanel();
            JPanel userPanel = new JPanel(new GridLayout(2,1));
            JPanel passPanel2 = new JPanel(new BorderLayout());
            JPanel passPanel = new JPanel(new GridLayout(2,1));
            JPanel bottomPanel = new JPanel(new BorderLayout());
            JPanel messagePanel = new JPanel();
            JPanel buttonPanel = new JPanel();
                cl = new CardLayout();
            cardPanel = new JPanel();
            cardPanel.setLayout(cl);
                cl2 = new CardLayout();
            cardPanel2 = new JPanel();
            cardPanel2.setLayout(cl2);
            GridBagConstraints a = new GridBagConstraints();   
            
            //Card Panels
            
            //New Profile
            NewProfile newProfileCreate = new NewProfile();
            JPanel newProfilePanel = newProfileCreate.createProfile();
            
            newProfilePanel.setOpaque(false);
            
            JPanel foregroundPanel = new JPanel(new BorderLayout(10, 10));
            foregroundPanel.setBorder(
                    BorderFactory.createEmptyBorder(10,10,10,10));
                    
            JLabel userLabel = new JLabel("[ Username ]",JLabel.CENTER);
            JLabel passLabel = new JLabel("[ Password ]",JLabel.CENTER);
            JLabel space = new JLabel("");
            
            spacePanel.setOpaque(false);
            fieldPanel.setOpaque(false);
            infoPanel.setOpaque(false);
            foregroundPanel.setOpaque(false);
            userPanel.setOpaque(false);
            passPanel.setOpaque(false);
            buttonPanel.setOpaque(false);
            passPanel2.setOpaque(false);
            bottomPanel.setOpaque(false);
            messagePanel.setOpaque(false);
            cardPanel.setOpaque(false);
            cardPanel2.setOpaque(false);
            
            JButton logonButton = new JButton    ("    Login    ");
            JButton toolsButton = new JButton    ("New User");
            JButton clearButton = new JButton    ("     Info     ");
            JButton exitButton = new JButton     ("     Exit     ");
            
            userNameField = new JTextField(30);
            userNameField.setFont(new Font("Arial Unicode MS", Font.BOLD, 12));
            userNameField.requestFocus();
            passwordField = new JPasswordField(30);
            passwordField.setFont(new Font("Arial Unicode MS", Font.BOLD, 12));
            defaultEcho = passwordField.getEchoChar();        
            
            a.gridx = 1;
            a.gridy = 2;
            
            buttonPanel.add(logonButton);
            buttonPanel.add(clearButton);
            buttonPanel.add(toolsButton);
            buttonPanel.add(exitButton);
            
            bottomPanel.add(buttonPanel,  BorderLayout.NORTH);
            bottomPanel.add(messagePanel, BorderLayout.SOUTH);
            
            userPanel.add(userLabel);
            userPanel.add(userNameField);
            passPanel.add(passLabel);
            passPanel.add(passwordField);
            infoPanel.add(userPanel, BorderLayout.NORTH);        
            passPanel2.add(passPanel, BorderLayout.CENTER);
            passPanel2.add(buttonPanel, BorderLayout.SOUTH);
            infoPanel.add(passPanel2, BorderLayout.SOUTH);
            fieldPanel.add(infoPanel);
            
            cardPanel.add(fieldPanel, "Logon Screen");
            cardPanel.add(newProfilePanel, "New Profile");
            
            spacePanel.add(cardPanel);
    
            foregroundPanel.add(spacePanel, BorderLayout.SOUTH);
    
            cardPanel2.add(foregroundPanel, "Logon Screen");
            
            mainWindow.add(cardPanel2);        
            
            String backGroundImageFile = "Default Logon Background.jpg"; 
            
            mainWindow.setContentPane(wrapInBackgroundImage(foregroundPanel,
                    new ImageIcon(
                    Logon.class.getResource(backGroundImageFile))));                
                   
            mainWindow.setUndecorated(true);
            mainWindow.setSize(900,563);
            mainWindow.setLocationRelativeTo(null);
            mainWindow.setVisible(true);        
            mainFrame2.dispose();
        }      
    Thanks,

    R
  • 6. Re: JAR file not executing fully
    843804 Newbie
    Currently Being Moderated
    P.S. Any variables used are correctly initiated even though it doesnt appear with this code. Again, tried it make it fit with no luck.
  • 7. Re: JAR file not executing fully
    793415 Pro
    Currently Being Moderated
    vt13 wrote:
    My main method and my showSplash method have no exception statements and neither declare that some may be thrown. ...
    OK, sure. And what happened when you run it from the command line to see all output?
  • 8. Re: JAR file not executing fully
    843804 Newbie
    Currently Being Moderated
    Sorry, should have just done that to begin with.

    Exception in thread "main" java.lang.NoClassDefFoundError: Logon
    Caused by : java.lang.ClassNotFoundException: Logon
    at java.net.URLClassLoader$1.run(Unkown Source)
    at java.security.AccessController.doPrivileded(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    Could not find the main class: Logon. Program will exit.
  • 9. Re: JAR file not executing fully
    793415 Pro
    Currently Being Moderated
    vt13 wrote:
    Sorry, should have just done that to begin with.
    Before we get too deeply into this, I just want to comment that you have better chance of getting help if you can boil the problem down to one single SSCCE, rather than all these 'bits and code snippets' you are posting. It is a big mistake to go typing hundreds of lines of code without testing it in small parts, and that is exactly what you have done.
    Exception in thread "main" java.lang.NoClassDefFoundError: Logon
    Caused by : java.lang.ClassNotFoundException: Logon
    ...
    That is odd.
    1) Did you have a package statement at the top of the code for Logon.java?
    2) What is the output of
    prompt: jar -tvf the.jar
  • 10. Re: JAR file not executing fully
    843804 Newbie
    Currently Being Moderated
    the only output is:

    : - tvf PortableWorkstation.jar
  • 11. Re: JAR file not executing fully
    843804 Newbie
    Currently Being Moderated
    No package statement either.

    The only reason Ive written so much code before running into this problem is because the whole thing runs fine in BlueJ. I assumed if it didnt cause problems there it wouldnt cause them as a .jar like any other program Ive made into a .jar before.
  • 12. Re: JAR file not executing fully
    793415 Pro
    Currently Being Moderated
    vt13 wrote:
    the only output is:

    : - tvf PortableWorkstation.jar
    So your telling me that the exact output is like this?
    prompt: jar -tvf PortableWorkstation.jar
    prompt: : - tvf PortableWorkstation.jar
    That does not seem right!
    No package statement either.
    OK. That rules out one possibility.
    The only reason Ive written so much code before running into this problem is because the whole thing runs fine in BlueJ. I assumed if it didnt cause problems there it wouldnt cause them as a .jar like any other program Ive made into a .jar before.
    So "live and learn" huh? ;)
  • 13. Re: JAR file not executing fully
    843804 Newbie
    Currently Being Moderated
    So your telling me that the exact output is like this?
    prompt: jar -tvf PortableWorkstation.jar
    prompt: : - tvf PortableWorkstation.jar
    That does not seem right!
    My command line says as follows:

    C:\Users\R\>prompt: jar -tvf PortableWorkstation.jar

    :jar - tvf PortableWorkstation.jar
    So "live and learn" huh? ;)
    Have to learn somehow, right? I'll almost bet I wont make the same mistake after this.
  • 14. Re: JAR file not executing fully
    793415 Pro
    Currently Being Moderated
    vt13 wrote:
    So your telling me that the exact output is like this?
    prompt: jar -tvf PortableWorkstation.jar
    prompt: : - tvf PortableWorkstation.jar
    That does not seem right!
    My command line says as follows:

    C:\Users\R\>prompt: jar -tvf PortableWorkstation.jar

    :jar - tvf PortableWorkstation.jar
    <head-desk />

    Java is x-plat, so I typed something that would hopefully make you realise that you needed to type "jar -tvf PortableWorkstation.jar" at the prompt of whatever command line interface you were using. My bad. To translate that into Windowspeak, do this..
    C:\Users\R\>jar -tvf PortableWorkstation.jar
    So "live and learn" huh? ;)
    Have to learn somehow, right?
    Yes, of course.
    ...I'll almost bet I wont make the same mistake after this.
    (chuckle) Famous last words. ;)
1 2 Previous Next