Java 7 and 8 path

If you install Java 8 on Windows environment, apparently it will put a registry value for “current JDK version” to be 8. If you have java.exe path set for Java 7, it will error on running time with “expected to be version 8” or something. Not cool.

Advertisements

Java Swing, Put Component In The Center

It has been quite some time since I used this :

Dimension SCREEN_SIZE = Toolkit.getDefaultToolkit().getScreenSize();
MainFrame mainFrame = new MainFrame();
int halfWidth = mainFrame.getWidth() / 2;
int halfHeight = mainFrame.getHeight() / 2;
mainFrame.setLocation(SCREEN_SIZE.width / 2 - halfWidth, SCREEN_SIZE.height / 2 - halfHeight);
mainFrame.setVisible(true);

Example for a Frame.

And apparently, you can use this one line instead:

mainFrame.setLocationRelativeTo(null); // or 'this' to parent

damn 😀

How to Ping a Server in Java

Thanks Réal Gagnon 🙂


import java.io.*;

import java.net.*;

 

public class ReachableTest {

 public static void main(String args[]) {

     try {

       InetAddress address = InetAddress.getByName("web.mit.edu");

       System.out.println("Name: " + address.getHostName());

       System.out.println("Addr: " + address.getHostAddress());

       System.out.println("Reach: " + address.isReachable(3000));

     }

     catch (UnknownHostException e) {

       System.err.println("Unable to lookup web.mit.edu");

     }

     catch (IOException e) {

       System.err.println("Unable to reach web.mit.edu");

     }

   }

}