Search code examples
javajava.util.scannerupdatecheck

update checking via internet in java program


My program has a check for updates and the update is linked to here and when i run the application, it says my program is out of date (the default message if your current launcher version is any different from the one online. But i tested it out to print what current version i have and what the most up to date version is, and they are the same. Here is my code:

package net.ogpc.updates;
import java.awt.BorderLayout;
import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Scanner;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import net.ogpc.GUI.Launcher;

public class UpdatesCheck {
    JFrame frame = new JFrame("Checking For Updates...");
    JLabel lb = new JLabel("Checking For Updates...");
    String build = Launcher.build;
    public UpdatesCheck() {
        frame.setSize(160, 50);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setUndecorated(true);
        frame.setAlwaysOnTop(true);
        frame.add(lb, BorderLayout.NORTH);
        updateCheck();
        frame.setVisible(true);
    }
    @SuppressWarnings("resource")
    public void updateCheck() {
        try {
            URL url = new URL("http://pastebin.com/raw.php?i=mWNT0sZa");
            Scanner s = new Scanner(url.openStream());
            String vs = s.nextLine();
            if (vs != build) { //there is an update!
                System.out.println("update found");
                int ans = JOptionPane.showConfirmDialog(null, "You seem to be using an outdated Launcher.. \nCurrent Version: " + vs + "   -   Newest Version: " + Launcher.build + "\nDo you want to update the launcher?", "UPDATE FOUND!", JOptionPane.YES_NO_OPTION);
                if (ans == JOptionPane.YES_OPTION) {
                    //go to update page
                    try {
                        openWebpage(url.toURI()); //open update page from website
                    } catch (URISyntaxException e) {
                        e.printStackTrace();
                    }
                    System.exit(0);
                } else if (ans == JOptionPane.NO_OPTION) {
                    //end application
                    JOptionPane.showMessageDialog(null, "You canceled the update!\nYou must have the most up to date verion of the launcher to run.", "You need to update!", JOptionPane.NO_OPTION);
                    System.exit(0);
                }
            }else if (vs == build){ //no update found
                frame.setVisible(false);
                Launcher.frame.setEnabled(true);
                Launcher.frame.setVisible(true);
            }
        }
        catch(IOException ex) {
           ex.printStackTrace();
        }
    }
    public static void openWebpage(URI uri) {
        Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
        if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
            try {
                desktop.browse(new URL("http://hahn2014.weebly.com/ogpc-2014-update.html").toURI());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        new UpdatesCheck();
    }
}

any help would be appreciated!


Solution

  • try

     if ( ! build.equals(vs) ) { //there is an update!
    

    don't do it the other way round, vs may be null. build won't, since it's a literal, I assume.