9 Replies - 18829 Views - Last Post: 01 July 2010 - 04:10 AM Rate Topic: -----

#1 ao241  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 21
  • Joined: 30-June 10

Invalid or corrupt jar file?

Posted 30 June 2010 - 02:53 PM

Hi all,

I've been developing a few applications in blue-j recently, and up till now I've had no problems turning them into jar files.

How ever today I went to turn my latest creation into a jar file, to end up with bluej crashing every time I attempt to turn it into a jar. I decided to test it out with a few other files, and sure enough some of them turn into jar's, others crash bluej and create this invalid jar file.

The error message I get if I try to open one of them is : Ivalid or corrupt jarfile C:\Users\Ashley\Desktop\gui.jar

So to test some things I copied and pasted my GUI class into a new project and low and behold it created perfectly. So Im guessing it is something to do with my second class, so if anyone could give me a hint over what Im missing I'd really appreciate it.

Thanks in advance for any help :)

p.s sorry for the poor design of the code its been a bit of a hack job, Im re-commenting it as fast as I can but I'm really impatient as Ive worked realy hard on this project.


This turns into a jar file with no problems.
import javax.swing.*;
import java.awt.Container;
/**
 * Builds the graphical user interface.
 * 
 * @author Ash
 * @version 1
 */
public class GUI
{
    

public static void main(String[] args)
{

GUI gui = new GUI();

} 
//GUI variables
    private JFrame frame = new JFrame("Grand exchange sniper");
    private Container contentPane = frame.getContentPane();
    private JPanel mainPanel = new JPanel();
    
    
    
    


public GUI()
    {   
        frame.setSize(300,300);
        //Set the size of the window and prevent the user from resisizing it.
        frame.setResizable(false);
        
        
        //Make the frame visible.
        frame.setVisible(true);

        

    }
    
   
}


This crashes bluej and creates a corupt jar file (when creating the jar I have specified that the GUI class is the main class to start from.
import javax.swing.*;
import java.awt.Container;
/**
 * Builds the graphical user interface.
 * 
 * @author Ash
 * @version 1
 */
public class GUI
{
    

public static void main(String[] args)
{

GUI gui = new GUI();

} 
//GUI variables
    private JFrame frame = new JFrame("Grand exchange snipa");
    private Container contentPane = frame.getContentPane();
    private JPanel mainPanel = new JPanel();
    
    //private JButton startButton = new JButton("Start monitoring");
    //private JButton topMiddleButton = new JButton("Stop monitoring");
    
    //private JMenuBar menubar = new JMenuBar();
    //private JMenu aboutMenu = new JMenu("About");
    
    private Checker checker = new Checker();


public GUI()
    {   
        frame.setSize(300,300);
        //Set the size of the window and prevent the user from resisizing it.
        frame.setResizable(false);
        
        
        //Make the frame visible.
        frame.setVisible(true);

        

    }
    
   
}

import java.io.BufferedReader;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.Timer;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.*;
import java.awt.Container;


/**
 * The checker class takes a url and gets the raw data from a site, then checks every
 * set amount of seconds to see if the grand exchange has updated.
 * 
 * @author Ash
 * @version beta
 */
public class Checker
{
    
    //All the URL's
    private URL appleTreeSeed = null;
    
    
    
    //All the url connections
    private URLConnection appleTreeSeedConnection = null;
    
    
    
    //All the buffered readers
    private BufferedReader appleTreeSeedData = null;
    
    
    
    //All the string builders
    private StringBuilder appleTreeSeedDataBuilder = null;
    
    
    
    //All the inputstream readers
    private InputStreamReader appleTreeSeedFeed = null;
    
   

    //All the relevant strings
    private String nextLine;
    private String currentPrice;
    private String data = "Market price:";
    
    private String startingAppleSeedPrice = "";
    
    
    
    private String currentAppleTreeSeedPrice = "";
    
    //All the variables relevant to the intervals of updates.
    private int delay = 10000; //10 seconds
    private Timer time;
   
   
    
    
    
    
    /**
     * Initialise the timer and call the start up to get the market price to compare.
     */
    public Checker()
    {
    
        
        
         class TimerListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            
            System.out.print("test");
            getAppleTreeSeedPrice();
        }
    }
        time =new Timer(delay, new TimerListener());
        time.stop();
        
    startup();
        
    }
    
   
    
    /**
     * Establish the connection and get 3 different items data to use to check the update.
     */
    public void startup()
    {
    //Set up the items url's up and grab the starting price.
    try {
        
        
        //add the urls.
        appleTreeSeed = new URL("http://services.runescape.com/m=itemdb_rs/Apple_tree_seed/viewitem.ws?obj=5283");
        
        
        //open the connections.
        appleTreeSeedConnection = appleTreeSeed.openConnection();
        
        
        //start the input feed on the connections
        appleTreeSeedFeed = new InputStreamReader( appleTreeSeedConnection.getInputStream());  
        
        
         //now create the buffered readers.
        appleTreeSeedData = new BufferedReader(appleTreeSeedFeed);
        
        
        
        //make the new string builders.
        appleTreeSeedDataBuilder = new StringBuilder();
        
    
        
    }
   
    catch(MalformedURLException e) {
        e.printStackTrace();
                                   } 
       
                  
    catch (IOException e1) {
        System.out.println("Error in reading data from jagex");
                           }
   
    initialiseAppleSeeds();
    
    
    
            
    }
   
   /**
    * Get the price to check of apple tree seeds.
    */ 
   public void initialiseAppleSeeds()
   {
       try {
           
           nextLine = appleTreeSeedData.readLine();
           while (nextLine != null)
        {
            appleTreeSeedDataBuilder.append(nextLine + "\n");
            nextLine = appleTreeSeedData.readLine();
            
            String test = appleTreeSeedDataBuilder.toString();
            
            int index1 = test.indexOf(data);
            if (index1 != -1)
            {
                //correct line with price is found
                currentPrice = test;
            }
        }
        appleTreeSeedData.close();
        String holder = "";
        //cut the string down to just the price.
        int index2 = currentPrice.indexOf(data);
        holder = currentPrice.substring(index2);
        holder = holder.substring(18, 20);
        
        startingAppleSeedPrice = holder;
        
          }
        
        catch(MalformedURLException e) {
        e.printStackTrace();
                                       } 
       
                  
        catch (IOException e1) {
        System.out.println("Error in reading data from jagex");
                               }
           
       getAppleTreeSeedPrice();
       time.start();
   }
   
   
 
    
    /**
     * Get the current apple tree seed price.
     */
    public void getAppleTreeSeedPrice()
    {
        try {
        
       appleTreeSeed = new URL("http://services.runescape.com/m=itemdb_rs/Apple_tree_seed/viewitem.ws?obj=5283");
        
        
        
        //open the connections.
        appleTreeSeedConnection = appleTreeSeed.openConnection();
        
        

        //start the input feed on the connections
        appleTreeSeedFeed = new InputStreamReader( appleTreeSeedConnection.getInputStream());  
        
        
        
        //now create the buffered readers.
        appleTreeSeedData = new BufferedReader(appleTreeSeedFeed);
        
  
        //make the new string builders.
        appleTreeSeedDataBuilder = new StringBuilder();
           nextLine = appleTreeSeedData.readLine();
           while (nextLine != null)
        {
            appleTreeSeedDataBuilder.append(nextLine + "\n");
            nextLine = appleTreeSeedData.readLine();
            
            String test = appleTreeSeedDataBuilder.toString();
            
            int index1 = test.indexOf(data);
            if (index1 != -1)
            {
                //correct line with price is found
                currentPrice = test;
            }
        }
        appleTreeSeedData.close();
        String holder = "";
        //cut the string down to just the price.
        int index2 = currentPrice.indexOf(data);
        holder = currentPrice.substring(index2);
        holder = holder.substring(18, 20);
        
        currentAppleTreeSeedPrice = holder;
        
          }
        
        catch(MalformedURLException e) {
        e.printStackTrace();
                                       } 
       
                  
        catch (IOException e1) {
        System.out.println("Error in reading data from jagex");
                               }
        
        
        checkUpdate();
        
    }
    
    
    /**
     * Compare the starting price with the current price pulled from the site
     * if they are not equal there has been an update.
     */
    public void checkUpdate()
    {
        
        
        if (currentAppleTreeSeedPrice.equals(startingAppleSeedPrice))
        {
           System.out.println("test");
        }
        else
        {
            
            System.out.println("there has been an update!");
            initialiseAppleSeeds();
            
            
        }
        
        
    }
    
    
   
}
     
     
     








Is This A Good Question/Topic? 0
  • +

Replies To: Invalid or corrupt jar file?

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10565
  • View blog
  • Posts: 39,097
  • Joined: 27-December 08

Re: Invalid or corrupt jar file?

Posted 30 June 2010 - 04:54 PM

Are your Checker and GUI classes contained in the same .java file? If so, this could be the problem, as each public class must be declared in its own .java file named as ClassName.java (ie., GUI.Java, Checker.java).
Was This Post Helpful? 0
  • +
  • -

#3 ao241  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 21
  • Joined: 30-June 10

Re: Invalid or corrupt jar file?

Posted 30 June 2010 - 05:03 PM

View Postmacosxnerd101, on 30 June 2010 - 03:54 PM, said:

Are your Checker and GUI classes contained in the same .java file? If so, this could be the problem, as each public class must be declared in its own .java file named as ClassName.java (ie., GUI.Java, Checker.java).


No, they are both contained in there own java file in the project root. I tried merging the two classes into one large class and still had no success creating the jar.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10565
  • View blog
  • Posts: 39,097
  • Joined: 27-December 08

Re: Invalid or corrupt jar file?

Posted 30 June 2010 - 05:07 PM

If your program runs fine on your IDE, then it could have something to do with invalid configurations for your project. You could try creating a new project and copy/pasting the code into it. Or better yet, you could use command line to build your JAR file. You will have a lot more control of your JAR this way.
Was This Post Helpful? 0
  • +
  • -

#5 ao241  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 21
  • Joined: 30-June 10

Re: Invalid or corrupt jar file?

Posted 30 June 2010 - 05:19 PM

View Postmacosxnerd101, on 30 June 2010 - 04:07 PM, said:

If your program runs fine on your IDE, then it could have something to do with invalid configurations for your project. You could try creating a new project and copy/pasting the code into it. Or better yet, you could use command line to build your JAR file. You will have a lot more control of your JAR this way.


Wow copied and pasted it and vwolah it worked! Thanks alot!

As for your comment on using the command line, I will definatly look into it tomorow when I wake up, thank you for the link.

Cheers for spending some time on this!
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10565
  • View blog
  • Posts: 39,097
  • Joined: 27-December 08

Re: Invalid or corrupt jar file?

Posted 30 June 2010 - 05:22 PM

You'll find when you work with external files that your IDE doesn't always include them in the JAR file, which creates errors. That's why it is safer to use command line, as you can manually include those files.

Glad I could help! :)
Was This Post Helpful? 0
  • +
  • -

#7 ao241  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 21
  • Joined: 30-June 10

Re: Invalid or corrupt jar file?

Posted 30 June 2010 - 05:25 PM

When you say external files does that mean the classes that are being imported? Or just any file related to the code (for example I aim to put a sound clip into my program eventualy)? Sorry if this may seem stupid everything Ive been taught about java has been in bluej and Ive never had that problem before.

This post has been edited by ao241: 30 June 2010 - 05:25 PM

Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10565
  • View blog
  • Posts: 39,097
  • Joined: 27-December 08

Re: Invalid or corrupt jar file?

Posted 30 June 2010 - 06:01 PM

I'm referring to dependency files like sound files, images, animations, etc.
Was This Post Helpful? 0
  • +
  • -

#9 ao241  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 21
  • Joined: 30-June 10

Re: Invalid or corrupt jar file?

Posted 01 July 2010 - 01:57 AM

View Postmacosxnerd101, on 30 June 2010 - 05:01 PM, said:

I'm referring to dependency files like sound files, images, animations, etc.


ok thanks :)

Could I ask you another question to?

I have everything working how I want it except one thing, when a user clicks on a button Id like to stop/remove the checker object from running, I cant seem to find out how to do this, Ive tried:

this.remove(checker);

But that doesnt work.

thanks again :)
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10565
  • View blog
  • Posts: 39,097
  • Joined: 27-December 08

Re: Invalid or corrupt jar file?

Posted 01 July 2010 - 04:10 AM

As the Checker Object isn't a GUI Component, you cannot remove it from the JFrame. And the only way you would be able to syntactically stop it would be if you were using something like a Thread, Swing Timer, Util Timer, SwingWorker, etc. As Swing isn't Thread-safe, I discourage the use of Thread and Util Timer. I also discourage the use of Swing Timer here, as it piggy-backs on the Event-Dispatch Thread, which means that the same built-in Thread to Swing that is responsible for handling Events will execute your Checker before it executes the button click. For these reasons, I suggest the use of SwingWorker. This will allow safe concurrency with Swing, including starting and stopping.

You could also design methods to access the next line from the URL in your Checker class, and invoke those in your GUI class. Then when the user hits the stop button, just don't invoke those related methods.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1