Jan 15 2015

Find JDK version used to compile a class

I had a situation where I needed to know what version of the JDK was used to compile a jar file. I can’t remember the details about why I needed to know but for whatever reason I needed to know. There are two ways to find out, one is super-simple and the other is simple as well, it just requires a few more steps.

The first way if you are on a *nix machine is to try the “file” command. More recent versions of “file” will identify the JDK used to compile a class file. I can vouch for the “file” command on Mac OS, it will show you the JDK version for a class file.

  1. First get a class file out of your jar file: jar xvf YourJarFile.jar path_to_class_file_in_jar
  2. Then: file SomeClass.class

Here is some sample output:

michael:~> file Client.class
Client.class: compiled Java class data, version 51.0 (Java 1.7)

If your file command doesn’t show the JDK version or you are on Windows (try Cygwin if you use Windows). Then you can examine the class file itself. Open the class file in some sort of hex editor. On a *nix machine you can use the hexdump command. The 8th byte of the class file contains the JDK version.

For example:

michael:~> hexdump SomeOther.class | head
0000000 ca fe ba be 00 00 00 34 00 20 08 00 14 0a 00 06

In this example the 8th byte is “34″ which is 52 decimal and 52 represents Java 1.8.

Here are the hex and decimal values for JDK versions:

Hex Decimal JDK Version
2E 46 Java 1.2
2F 47 Java 1.3
30 48 Java 1.4
31 49 Java 5
32 50 Java 6
33 51 Java 7
34 52 Java 8

Aug 19 2014

POST with Apache HttpClient 4

I recently started using Apache’s HttpClient 4, I was previously using version 3 and I discovered there is a vast difference in the APIs between the two versions. None of the documentation I found for HttpClient 4 had a full example of using POST with connection and socket timeout set. From the documentation I did find and the JavaDoc I pieced together this full example. For this code I used httpclient-4.3.2.jar which also needed httpcore-4.3.1.jar. I use IVY for dependency management and this entry in ivy.xml got me what I needed:

<dependency org="org.apache.httpcomponents" name="httpclient" rev="4.3.2"/>

Take a look at the Javadoc for RequestConfig.Builder for what other options are available.

If you need to convert the InputStream from the response to a String I have a quick how-to for that.

 public void executePost(String destinationUrl, Map<String, String> pairs) {
        //Set any needed config options, here is how you would set connection and socket timeout
        RequestConfig.Builder configBuilder = RequestConfig.custom();

        //Timeout for getting a connection to the web server

        //Timeout for data from web server once a connection is established

        //Create a List of BasicNameValuePairs, here I am doing it from a Map that was passed in. Do whatever
        //it is you need to do
        List<NameValuePair> nameValuePairs = new ArrayList<>(pairs.size());
        for (Map.Entry<String, String> entry : pairs.entrySet()) {
            nameValuePairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));

        //Create an HttpPost and set your config builder here if you have any custom settings
        HttpPost post = new HttpPost(destinationUrl);

        try {
            //Create an encoded form entity and assign it to the HttpPost object
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nameValuePairs, Consts.UTF_8);

            //Execute the post, this returns an HttpResponse if all is well or throws an exception if one of the
            //timeout values are met
            HttpResponse httpResponse = this.localHttpClient.get().execute(post);
            int statusCode = httpResponse.getStatusLine().getStatusCode();
            if (statusCode >= 200 && statusCode <= 299) {
                //2xx is success, handle them as success. Or handle specific status codes, whatever the case
                //may be

                InputStream responseStream = httpResponse.getEntity().getContent();
                //Do something with the responseStream, convert it to a String, pass it to an XML parser, whatever
        } catch (Exception e) {
        } finally {

Aug 19 2014

Quick How-To: Convert InputStream to String

I find myself having to convert an InputStream to a String on occasion. It isn’t something I do super-frequently so I usually have to go hunt down some code where I have done it before. Here it is for quick reference for myself and whoever else. This code uses a Java 1.7 try-with-resources statement. If you aren’t using Java 1.7+ you will of course need to use the correct try syntax for prior versions of Java with a finally block.

 public String readBody(InputStream inputStream) throws Exception {
        StringWriter writer = new StringWriter();

        char[] buffer = new char[1024];
        try (Reader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"))) {
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);

        return writer.toString();

Jul 05 2013

Subversion: Compile Subversion 1.8 on Mac OS 10.8 or 10.9

To compile Subversion 1.8 on Mac OS 10.8 (Mountain Lion) only requires the compilation of one dependency. Prior to 1.8 Subversion used neon for HTTP requests. It now uses Serf. If you access any repositories via HTTP you must first download and compile Serf.

If you compile Subversion without Serf being present and then try to access a repository via HTTP you get an error that looks something like this:

svn: Unrecognized URL scheme for http://www.example.com/svn/some_respository

Compiling Serf

First you need to download Serf which can be downloaded from http://code.google.com/p/serf/downloads/list. I used version 1.2.1 which was the most recent at the time I compiled it.

Generally compiling something is as easy as ./configure && make && sudo make install; however, for some reason Serf tries to use the wrong compiler by default. I am sure a C programmer could explain why; however, I am not one so I won’t attempt to explain. I will just tell you how to fix it. If you try to run “./configure” you will get the error message:

checking for gcc… /Applications/Xcode.app/Contents/Developer/Toolchains/OSX10.8.xctoolchain/usr/bin/cc
checking whether the C compiler works… no
configure: error: in `/Users/mjparme/temp/serf-1.2.1′:
configure: error: C compiler cannot create executables
See `config.log’ for more details

To fix this you need to create a symbolic link to point it at the correct compiler, so execute this command:

sudo ln -s /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain /Applications/Xcode.app/Contents/Developer/Toolchains/OSX10.8.xctoolchain

For Mavericks (10.9) this symbolic link should be

sudo ln -s /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain /Applications/Xcode.app/Contents/Developer/Toolchains/OSX10.9.xctoolchain

For Yosemite (10.10) this symbolic link should be (thanks to the commenter for this!):

sudo ln -s /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain /Applications/Xcode.app/Contents/Developer/Toolchains/OSX10.10.xctoolchain

Now you can run ./configure with no errors.

  1. Donwload Serf http://code.google.com/p/serf/downloads/list
  2. Unzip the downloaded file: unzip serf-1.2.1.zip (or “tar xvfz serf-1.2.1.tar.bz2″ if you downloaded the bzip one)
  3. cd serf-1.2.1
  4. Execute: ./configure && make -j 5 && sudo make install

Note that the value you pass for the -j parameter should be number of cores + 1 (this is the general consensus and my totally unscientific benchmarking supports the consensus). So if you have a dual-core machine, pass 3, quad-core pass 5, etc.

Also note that newer versions of the tar command can unzip files compressed with bzip2 as well as gzip with the “z” option.

Compile Subversion 1.8

Now since the Serf stuff is taken care of you can compile Subversion 1.8 that includes HTTP support like you would most any other app written in C.

  1. Download the source here: http://subversion.apache.org/download/
  2. Unzip the downloaded file: unzip subversion-1.8.0.zip (or “tar xvfz subversion-1.8.0.tar.gz” or “tar xvfz subversion-1.8.0.tar.bz2″ depending on which one you downloaded)
  3. cd subversion-1.8.0
  4. ./configure –with-serf=/usr/local/serf && make -j 5 && sudo make install

By default Subversion is installed in /usr/local/bin. Note that Mac OS 10.8 comes with subversion 1.6.18 installed in /usr/bin. To insure your compiled version is the one executed make sure /usr/local/bin appears in your PATH before /usr/bin.

So if you are using bash as your shell, add this to .bashrc in your home directory:

export PATH

Jul 03 2013

Weblogic: Grails index.gsp not found

If you try to deploy a Grails application to Weblogic and try to go to “/” in the app you will get the message:

“/index.gsp” not found.

This definitely affects Weblogic 9. and 10.x. I am unsure if it is a problem with later versions. The fix is a simple addition to the UrlMappings.groovy file. Add the line:


So that it looks like this:

class UrlMappings {
	static mappings = {
			constraints {
				// apply constraints here




Older posts «