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

    Help needed: getting jar files to work.

    800663
      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();
                page.show();
           }
      }
      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",
                     "prayer","crafting","firemaking","magic","fletching","woodcutting","runecraft","slayer","farming","construction","hunter","summoning"};
           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,
                     8740,9730,10824,12031,13363,14833,16456,18247,20224,22406,24815,27473,30408,33648,37224,41171,45529,50339,55649,61512,67983,
                     75127,83014,91721,101333,111945,122660,136594,150872,166636,184040,203254,224466,247886,273742,302288,333804,368599,407015,
                     449428,496254,547953,605032,668051,737627,814445,899257,992895,1096278,1210421,1336443,1475581,1629200,1798808,1986068,2192818,
                     2421087,2673114,2951373,3258594,3597792,3972294,4385776,4842295,5346332,5902831,6517253,7195629,7944614,8771558,9684577,10692629,
                     11805606,13034431,14567891};
           URL page;
           static String name;
           JLabel[] skills  = new JLabel[24];
           JLabel skillicons  = new JLabel();
           public PageFrame(){
                
                super("Stats Lookup");
                setSize(800,600);
                setResizable(false);
                try{
                     UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
                     SwingUtilities.updateComponentTreeUI(this);
                     
                }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();
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                GridBagConstraints constraints = new GridBagConstraints();
                
                JPanel skillPane = new JPanel();
                GridBagLayout levelslyo = new GridBagLayout();
                GridBagConstraints sConstraints = new GridBagConstraints();
                sConstraints.ipadx = 15;
                sConstraints.ipady = 10;
                skillPane.setLayout(levelslyo);
                int k = 0;
                int m = 1;
                buildconstraints(sConstraints,12,4,1,1,2,2);
                skillPane.add(skillicons);
                for (int i = 0; i < skills.length;i++){
                     
                     buildconstraints(sConstraints,m,k,1,1,2,2);     
                          levelslyo.setConstraints(skills, sConstraints);
                          //skills[i].setText(" 0 ");
                          skillPane.add(skills[i]);
                          skills[i].addMouseListener(this);
                          m+=2;                    
                          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);
                     buildconstraints(sConstraints,m,k,1,1,2,2);     
                     levelslyo.setConstraints(skillicons, sConstraints);
                     
                     skillicons = new JLabel(icon);
                     skillPane.add(skillicons);

                          m+=2;                    
                          if (m == 6){
                               m = 0;
                               k +=1;
                          }
                          
                          
                }*/
                buildconstraints(constraints,0,0,1,1,100,100);
                maingrid.setConstraints(username, constraints);
                buildconstraints(constraints,1,0,1,1,100,100);
                maingrid.setConstraints(search, constraints);
                buildconstraints(constraints,0,1,1,1,100,100);
                maingrid.setConstraints(skillPane, constraints);
                pane.setLayout(maingrid);
                constraints.fill = GridBagConstraints.BOTH;          
                search.addActionListener(this);
                box.setEditable(false);
                buildconstraints(constraints,1,1,2,1,120,100);
                maingrid.setConstraints(box, constraints);
                pane.add(username);
                pane.add(search);
                pane.add(box);
                pane.add(skillPane);
                
                setContentPane(pane);
                //getContentPane().add(scroll);
                pack();
                setVisible(true);
                WindowListener l = new WindowAdapter(){
                     public void windowClosing(WindowEvent evt){
                          System.exit(0);
                     }
                };
                addWindowListener(l);
                
                
                
           }

           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();
                try{
                     conn = this.page.openConnection();
                     conn.connect();
                     //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},
                                    {14,5},{15,2},{16,7},{17,4},{17,10},{19,19},{20,20},{21,18},{22,22},{23,21},{24,23}};
                          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());                         
                          }                    
                          ts++;
                     }               
                     for (int i = 0; i < skills.length;i++){
                          String text = ""+stats.levels[i];//testings[i];//
                          skills[i].setText(text);
                     }
                
                }catch(IOException e){
                     box.setText("Insert a username \nfirst!");
                }
           }
           public String getNextXp(int level, int currxp){
                String nextXp;
                if (level > 98){
                     nextXp = "N/A";
                }else{
                     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;
                     try{
                          page = new URL(address);               
                          getStats(page);
                     }catch(MalformedURLException e){
                          box.setText("Insert a username");
                     }
                     repaint();
                
           }
           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) {
      box.setText("");
      }
           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.
          807591
          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.
            800663
            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.
              807591
              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++){
                  System.out.println(
                          "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.
                800663
                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.
                  807591
                  ...

                  Edited by: pbrockway2 on May 18, 2008 12:43 PM
                  • 6. Re: Help needed: getting jar files to work.
                    807591
                    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.
                      800663
                      Thank You all very much. I got it work :) Also made exe with JSmooth and it works.