Click here to Skip to main content
15,895,799 members
Articles / Programming Languages / Java
Tip/Trick

NetChecker in Java

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
22 Dec 2010CPOL2 min read 7.1K   28   5  
A Java applet to check if .NET 3.5 is installed

Introduction


My programs are mostly stand-alone, so they don't have an installer but are download in a zip file. The problem is, this way I can't check if my users have the .NET Framework 3.5 installed and I know most of them don't when they first visit my site. I used to check the useragent of Internet Explorer, but because of the limited use I searched for something else. Eventually I came up with the idea of a Java applet.


Background


This is the first Java code I ever wrote, so maybe it isn't the best. The applet itself was pretty soon finished, but my biggest problems were related to security. I had to sign the applet to make it work and then I needed to create a custom manifest to allow signed and unsigned code being used in one applet.


Using the Code


I used to check the IE useragent with PHP as follows.


find = strpos($_SERVER['HTTP_USER_AGENT'], '.NET CLR 3.5');
if ($find == 0) {
  print 'The .NET Framework v3.5 is not found!';
} else {
  print 'The .NET Framework v3.5 is installed.';
}

The Java applet checks it another way. It searches for the "%windir%\Microsoft.NET\Framework\v3.5\" folder.


Java
import java.io.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.applet.*;
public class NetCheck  extends JApplet implements ActionListener
{
      private JButton btnGo = null;
      private JPanel pane = new JPanel();
      private final JLabel label = new JLabel();
      private final String GO = "Go";
       
      public void init()
      {
        try
        {
          Init();
        } catch(Exception e)
        {
          e.printStackTrace();
        }
      }
      private void Init() throws Exception
      {
        btnGo = new JButton();
        btnGo.setBounds(new Rectangle(96, 10, 75, 23));
        btnGo.setText("Check");
        btnGo.setActionCommand(GO);
        btnGo.addActionListener(this);
        btnGo.setBackground(new Color(255, 138, 0));
        btnGo.setForeground(new Color(42, 42, 42));
        pane.setBackground(new Color(42, 42, 42));
        pane.setBounds(new Rectangle(0, 0, 267, 44));
        pane.setLayout(null);
        pane.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
        pane.add(btnGo);
        setContentPane(pane);
        setSize(267, 44);
      }
      
      public void actionPerformed(ActionEvent e)
      {
        if (e.getActionCommand().equals(GO))
        {
          pane.remove(btnGo);
          label.setForeground(new Color(234, 234, 234));
          label.setBounds(47, 18, 215, 16);
          File file = new File(System.getenv("windir") + "\\Microsoft.NET\\Framework\\v3.5\\");
          Image img;
          if (!file.exists())
          {
            label.setText("The .NET Framework v3.5 is not found!");
            img = getImage(getCodeBase(), "Images/No.png");
          } else
          {
            label.setText("The .NET Framework v3.5 is installed.");
            img = getImage(getCodeBase(), "Images/Yes.png");
          }
          DrawingPanel drawing_panel = new DrawingPanel(img);
          drawing_panel.setBounds(10, 10, 32, 32);
          pane.add(label);
          pane.add(drawing_panel);
          this.repaint();
        }
      }
      class DrawingPanel extends JPanel 
      {
        Image img; 
        DrawingPanel(Image img) 
        {
          this.img = img;
        } 
        public void paintComponent(Graphics g)
        { 
          super.paintComponent(g);    
          g.drawImage(img, 0, 0, this); 
        }
      }
}

As you can see, the code is fairly simple. The message, images and colors are easy adjustable if needed.


The next part requires the JDK. As said, the application has to be signed to prevent any security problems. Without signing, it won't run at all and just give an exception. For your convenience, I build a batchfile to easy sign the application. Just put the sourcecode, the manifest and the batchfile in the JDK\bin folder and run Build.bat.


The signing has to be done with a custom created manifest (You can just open the file in Notepad if you want), to allow signed and unsigned code to run in the same applet.


Points of Interest


Despite all the effort to get the application signed, the user will still get a warning about an unverified signing. The only solution is to buy an expensive (At least, for a hobbyist...) signing certificate from a trusted certificate authority.
Even then, the user is asked for permission to run the applet. With a good guideline (as seen at the installation of Flash, for example), I don't think it's such a big problem. After all, users are more vulnerable when they download the final application than when they check if they can even run it with a Java applet...


Because it is my first Java code, I needed a lot of documentation and I would like to thank some sites that helped me out.


http://www.developer.com/article.php/3303561
http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter06/images.html
http://download.oracle.com/javase/6/docs/technotes/guides/jweb/mixed_code.html#manifest
http://www.roseindia.net/java/beginners/file-directory-exists.shtml


History



  • 22nd March, 2010: Initial post.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --