7 Replies Latest reply: May 18, 2008 8:55 AM by 800663 RSS

    Help needed: getting jar files to work.

      Right, to start off, i have been doing php and mysql for a long time and i thought it's time to start using java.
      So now im trying to create an application. I use eclipse. All works well if i run the program from eclipse. But i want to create and executable or a jar file first. I used JSmooth but with that i get an error "Could not find the main class".
      While exporting the project as a jar i specified the main class in manifest specification.
      So these are my classes:
      the Main.class
      public class Main{
           public static void main(String[] args)  {
                PageFrame page = new PageFrame();
      and the PageFrame.class
      import java.net.*;
      import java.io.*;
      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
      import java.util.*;
      class PageFrame extends JFrame implements ActionListener,MouseListener{
           String[] skillnames = {"attack","hitpoints","mining","strength","agility","smithing","defence","herblore","fishing","ranged","thieving","cooking",
           JTextField username = new JTextField("",12);
           JTextArea box = new JTextArea(10,12);
           JButton search = new JButton("Search");
           int[] differences = {0,83,174,276,388,512,650,801,969,1154,1358,1584,1833,2107,2411,2746,3115,3523,3973,4470,5018,5624,6291,7028,7824,
           URL page;
           static String name;
           JLabel[] skills  = new JLabel[24];
           JLabel skillicons  = new JLabel();
           public PageFrame(){
                super("Stats Lookup");
                }catch (Exception e){
                     System.err.println("Yo" + e);
                //teen labelid;
                for (int i = 0; i < skills.length;i++){
                     ImageIcon icon = new ImageIcon(PageFrame.class.getResource("images/skillimages/" + skillnames[i] + ".gif"));
                     skills[i] = new JLabel("00",icon,JLabel.CENTER);
                JPanel pane = new JPanel();
                GridBagLayout maingrid = new GridBagLayout();
                GridBagConstraints constraints = new GridBagConstraints();
                JPanel skillPane = new JPanel();
                GridBagLayout levelslyo = new GridBagLayout();
                GridBagConstraints sConstraints = new GridBagConstraints();
                sConstraints.ipadx = 15;
                sConstraints.ipady = 10;
                int k = 0;
                int m = 1;
                for (int i = 0; i < skills.length;i++){
                          levelslyo.setConstraints(skills, sConstraints);
                          //skills[i].setText(" 0 ");
                          if (m == 7){
                               m = 1;
                               k +=1;
                /*m = 0;
                k = 0;

                for (int i = 0; i < skills.length;i++){          
                     System.out.println("Paremale: " + m + " Alla: " + k);
                     levelslyo.setConstraints(skillicons, sConstraints);
                     skillicons = new JLabel(icon);

                          if (m == 6){
                               m = 0;
                               k +=1;
                maingrid.setConstraints(username, constraints);
                maingrid.setConstraints(search, constraints);
                maingrid.setConstraints(skillPane, constraints);
                constraints.fill = GridBagConstraints.BOTH;          
                maingrid.setConstraints(box, constraints);
                WindowListener l = new WindowAdapter(){
                     public void windowClosing(WindowEvent evt){

           void buildconstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx,int wy){
                gbc.gridx = gx;
                gbc.gridy = gy;
                gbc.gridwidth = gw;
                gbc.gridheight = gh;
                gbc.weightx = wx;
                gbc.weighty = wy;
           /*Vector ranks = new Vector();
           Vector levels = new Vector();
           Vector exp = new Vector();*/
           class Stats{
                int[] levels = new int[34];
                int[] experience = new int[34];
                int[] ranks = new int[34];
           Stats stats = new Stats();
           public void getStats(URL url){          
                URLConnection conn = null;
                InputStreamReader in;
                BufferedReader data;
                String line;          
                StringBuffer buf = new StringBuffer();
                     conn = this.page.openConnection();
                     //box.setText("Connection opened");
                     in = new InputStreamReader(conn.getInputStream());
                     data = new BufferedReader(in);
                     //box.setText("Reading data...");
                     int ts=0;
                     while ((line = data.readLine()) != null){
                          buf.append(line + "\n");
                          StringTokenizer tokenize;
                          tokenize = new StringTokenizer(line.toString(),",");
                          int[][] paarid = {{1,0},{2,3},{3,6},{4,1},{5,9},{6,12},{7,15},{8,11},{9,17},{10,16},{11,8},{12,14},{13,13},
                          if (ts < 25 && ts > 0){
                               int slot = paarid[ts-1][1];
                                    stats.ranks[slot] = Integer.parseInt(tokenize.nextToken());
                                    int yo = Integer.parseInt(tokenize.nextToken());
                                    stats.levels[slot] =yo;
                                    stats.experience[slot] = Integer.parseInt(tokenize.nextToken());                         
                     for (int i = 0; i < skills.length;i++){
                          String text = ""+stats.levels[i];//testings[i];//
                }catch(IOException e){
                     box.setText("Insert a username \nfirst!");
           public String getNextXp(int level, int currxp){
                String nextXp;
                if (level > 98){
                     nextXp = "N/A";
                     nextXp = ""+(differences[level] - currxp);
                return nextXp;
           public void actionPerformed(ActionEvent evt){
                     String newuser = username.getText().toString();
                     name = newuser;
                     String address = "http://hiscore.runescape.com/index_lite.ws?player="+name;
                          page = new URL(address);               
                     }catch(MalformedURLException e){
                          box.setText("Insert a username");
           public void mouseEntered(MouseEvent e) {
                //if (experience[].isEmpty()){          
                     Object s = e.getSource();
                     for (int i = 0; i < skills.length;i++){
                          if (s == skills[i]){
                               box.setText("Rank: " + stats.ranks[i] + "\nExperience: " + stats.experience[i] + "\nExp until next: " + getNextXp(stats.levels[i],stats.experience[i]));

           public void mouseExited(MouseEvent e) {
           public void mouseClicked(MouseEvent e) {
           public void mousePressed(MouseEvent e) {


           public void mouseReleased(MouseEvent e) {
      This is just a little project i thought i'd make for learning.
      But can anyone help me make a working jar or an exe file?
      The Jar i created does nothing, no error or anything. 
      Thanks for Help.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
        • 1. Re: Help needed: getting jar files to work.
          But can anyone help me make a working jar or an exe file?
          There are step by step instructions in the [Jar Files|http://java.sun.com/docs/books/tutorial/deployment/jar/index.html] section of Sun's Tutorial.
          The Jar i created does nothing, no error or anything.
          How are you running the program? On Windows double clicking often leads to the javaw.exe executable being run, and you will miss any command line output.
          • 2. Re: Help needed: getting jar files to work.
            I read that tutorial, but i exported it as a JAR from eclipse. I don't have command line output there, or atleast i don't need to see it now.
            How should i run the jar file then? I am double clicking it yes.

            EDIT: i ran the program through command line and i got an error:
            Exception in thread "main" java.lang.NullPointerException
            at javax.swing.ImageIcon.<init>(Unknown Source)
            at PageFrame.<init>(PageFrame.java:41)
            at Main.main(Main.java:4)

            I don't get that in eclipse tho. What can be the problem?

            Edited by: Cheff88 on May 17, 2008 4:57 PM
            • 3. Re: Help needed: getting jar files to work.
              How should i run the jar file then? I am double clicking it yes.
              EDIT: i ran the program through command line and i got an error:
              Exception in thread "main" java.lang.NullPointerException
              at javax.swing.ImageIcon.<init>(Unknown Source)
              at PageFrame.<init>(PageFrame.java:41)
              at Main.main(Main.java:4)
              OK - so it looks like you've found a command line!

              You read these stack traces from the top down. Often the most interesting line is the first line that refers to your code. In this case line 41 of PageFrame.java which, I guess is this:
              ImageIcon icon = new ImageIcon(PageFrame.class.getResource(
                      "images/skillimages/" + skillnames[i] + ".gif"));
              The ImageIcon constructor is grumbling about being told to construct an icon from a null resource. getResource() will return null if it cannot find the resource that you specify.

              The first thing to do is check that the .gif image is where it should be. You have specified it as "images/skillimages/etc" so use the command line to view the contents of the .jar file (or maybe Eclipse will let you do this). Next to PageFrame.class there should be a folder "images" and within it a folder "skillimages" and within that the image file. If not then your jar export has been seen up wrongly and is not including the required resource files.

              (The "View" command is explained here: [http://java.sun.com/docs/books/tutorial/deployment/jar/view.html].)

              Next check the case of the file names. On Windows getResource() will not worry about the cAsE of filenames so the resources will be located fine when running the program unjarred (including within Eclipse). But it does worry about the case of jar entry names. Bottom line: keep the file names all lower case because you use them that way in the code.

              Finally, in case it is a particular image resource that's causing the problem you could add some code to print out its name just before you try and create the icon.
              for (int i = 0; i < skills.length;i++){
                          "About to make icon from " + "images/skillimages/" + skillnames[i] + ".gif");
                  ImageIcon icon = new ImageIcon(PageFrame.class.getResource(
                          "images/skillimages/" + skillnames[i] + ".gif"));
                  skills[i] = new JLabel("00",icon,JLabel.CENTER);
              • 4. Re: Help needed: getting jar files to work.
                This is getting too funny...
                I think i fixed that problem, i checked the contents and yes, the folders are different, the images are in src/images/skillimages folder. The cases are Okay.
                But now, i get another error and i don't understand thatone at all.
                Exception in thread "main" java.lang.NoClassDefFoundError: skillimages/jar
                Caused by: java.lang.ClassNotFoundException: skillimages.jar
                        at java.net.URLClassLoader$1.run(Unknown Source)
                        at java.security.AccessController.doPrivileged(Native Method)
                        at java.net.URLClassLoader.findClass(Unknown Source)
                        at java.lang.ClassLoader.loadClass(Unknown Source)
                        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
                        at java.lang.ClassLoader.loadClass(Unknown Source)
                        at java.lang.ClassLoader.loadClassInternal(Unknown Source)
                I have no idea :/
                • 5. Re: Help needed: getting jar files to work.

                  Edited by: pbrockway2 on May 18, 2008 12:43 PM
                  • 6. Re: Help needed: getting jar files to work.
                    Exception in thread "main" java.lang.NoClassDefFoundError: skillimages/jar
                    Caused by: java.lang.ClassNotFoundException: skillimages.jar
                    What is the command you are using to run the program?

                    It looks like you are saying
                    java skillimages.jar
                    or something (eg using javaw, or have such a command configured to run when the icon is double clicked).

                    The command should look something like:

                    (a) java -jar skillimages.jar (if the main class is specified in the manifest)
                    (b) java -cp skillimages.jar Main (if not)
                    • 7. Re: Help needed: getting jar files to work.
                      Thank You all very much. I got it work :) Also made exe with JSmooth and it works.