Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Java not found or wrong java version - update-alternatives on Linux environment


Hola!

OK so let's rock... here are the possible problems:
0. No java / javac found.
1. Somehow you have two or more java (JRE or/and JDK) installed on the system.
2. Setting JAVA_HOME to your preferred version does not help as some stuff still use system default version
3. Cannot find javac or java even if JAVA_HOME set.


Strange but can happen if more than one version installed.

Time for some fixes:
Let say your preferred JAVA_HOME is pointing to /home/user/jdk

For setting JAVA_HOME and PATH please refer to the post:
Setting JAVA_HOME environment variable

System alternatives and updates

1. Install some java alternatives:
sudo update-alternatives --install /usr/bin/java java /home/user/jdk/bin/java 1
sudo update-alternatives --install /usr/bin/javac javac /home/user/jdk/bin/javac 1
sudo update-alternatives --install /usr/bin/javac javaws /home/user/jdk/bin/javaws 1

2. Set / notify the system:
sudo update-alternatives --set java /home/user/jdk/bin/java
sudo update-alternatives --set javac /home/user/jdk/bin/javac
sudo update-alternatives --set javaws /home/user/jdk/bin/javaws

3. Set the preferred one if have more alternatives
sudo update-alternatives --config java
sudo update-alternatives --config javac
sudo update-alternatives --config javaws


This is it! You should be up an running with your old new great JAVA world :)


JAVA - getting server date from remote host

Date objects in Java are quite common. Usually if you want to get the current date and time you just need to create new instance of java.util.Date:

new Date();

It is simply and straight forward in most cases. It returns whatever current date is on the client machine. 

But there might be a case when you want to have the actual date from the remote server. It is useful for example if you have trial version of your software and would like to check the real date or in general if the time is very important thing in your application.

Remote host date and time can be get from connection header. Example code below:
--------------------------------------------------------------------------------------------------------------------
01. Date remoteDate = null;
02. URL url = new URL(REMOTE_SERVER_URL);
03. URLConnection urlConn = url.openConnection();
04. HttpURLConnection conn = (HttpURLConnection) urlConn;
05. conn.setConnectTimeout(10000);
06. conn.setReadTimeout(10000);
07. conn.setInstanceFollowRedirects( true );
08. conn.setRequestProperty( "User-agent", "spider" );
09. conn.connect();
20. Map<String,List<String>> header = conn.getHeaderFields();
21. for (String key : header.keySet()) {
22.     if (key != null && "Date".equals(key)) {
23.         List<String> data = header.get(key);
24.         String dateString = data.get(0);
25.         SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss");
26.         remoteDate = sdf.parse(dateString);
27.         break;

28.     }
29. }
--------------------------------------------------------------------------------------------------------------------

Some notes for this code example:

Line 22. Some of the key from header map fields may be null and we are interested only in 'Date' field.

Line 25. This one depends on the date format returned by the server. In this example it is valid for example for:
Sat, 13 Oct 2012 17:57:50

And this it - you got a date :) 


How to make a column header immovable in JTable

I am not fulltime Swing developer but I have come to this little problem quite lately.  JTable columns headers are movable by default which sometimes can be annoying. Even if you do not want to move them you can do it by accident when for example trying to sort the column data.

Actually solution is pretty simple. Just use a custom table header renderer :)

It can be something like this one:
-----------------------------------------------------------------------------------------------------------------------
public class UnmovableTableHeader extends JTableHeader {

    public UnmovableTableHeader(TableColumnModel model) {
        super(model);
    }

    @Override
    public void setDraggedColumn(TableColumn aColumn) {
        super.setDraggedColumn(null); // it does the trick :)
    }

}
-----------------------------------------------------------------------------------------------------------------------


Then it can be set directly on JTable object. Assuming there is somewhere an instance of JTable called table:

TableColumnModel cm = table.getColumnModel();
UnmovableTableHeader th = new UnmovableTableHeader(cm)
table.setTableHeader(tableHeader);

That's all - columns cannot be dragged but you can still use them (i.e. sorting).

Java: StringBuilder versus StringBuffer

When it comes to building / joining the strings there are some options. The most popular way is to use '+' operator. However it is not always a good idea because in some circumstances it can cause out of memory related errors. The reason behind that is that whenever you using '+' operator the new string object is created. If this is done in a long loop you may get into troubles ;)

The other options are to use existing Java classes: StringBuilder or StringBuffer.

StringBuilder was introduced in Java 5 so there is a good chance that some old geeks are get used to StringBuffer instead.

In most cases StringBuilder should be used. It is more efficient than his older brother but it does not support multi-threaded environment. In other hand StringBuffer is synchronized and should be used multithreads.

But please do not give up on using '+' operator - it is still good way when there are no loops and when it comes to quickly join some lines intro a string.