I have a project from which I build a small library jar containing a subset of the project’s classes. The
application is a client/server application and most clients of the server are also Java applications. The
library helps us stay DRY so we don’t have to duplicate code in the client applications. The project
was recently changed from an Ant+Ivy build to Gradle 2.14 and we publish our internal libraries to our
own Nexus repository which is a Maven formatted repository. Publishing a secondary artifact
(i.e. not the main artifact of the project) to Nexus with Gradle turned into an exercise of frustration
and almost made me say “fuck it” and stick with Ant+Ivy (where this is trivial).
I had some issues getting a Grails 3 application to deploy to Tomcat 7.0.37. I finally figured out
that the problem was that I was running Tomcat with a 1.8 JDK. If you try to deploy a Grails 3 application to
Tomcat 7.0.37 running with a Java 1.8 JDK you get an error that looks like this when the application is deploying:
Caused by: org.apache.tomcat.util.bcel.classfile.ClassFormatException: Invalid byte tag in constant pool: 18
at org.apache.tomcat.util.bcel.classfile.Constant.readConstant(Constant.java:131)
at org.apache.tomcat.util.bcel.classfile.ConstantPool.<init>(ConstantPool.java:60)
at org.apache.tomcat.util.bcel.classfile.ClassParser.readConstantPool(ClassParser.java:209)
at org.apache.tomcat.util.bcel.classfile.ClassParser.parse(ClassParser.java:119)
at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:2032)
at org.apache.catalina.startup.ContextConfig.processAnnotationsJar(ContextConfig.java:1923)
at org.apache.catalina.startup.ContextConfig.processAnnotationsUrl(ContextConfig.java:1891)
at org.apache.catalina.startup.ContextConfig.processAnnotations(ContextConfig.java:1877)
at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1270)
at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:855)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:345)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5161)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
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.
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:
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.
Subversion: Compile Subversion 1.8 on Mac OS 10.8, 10.9, 10.10, and 10.11
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
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:
Grails: Deploy Grails 2.x or newer application to Weblogic 9
If you try to deploy a Grails application built with Grails 2.0 or greater to Weblogic 9.0 it will fail. There are two things you need to do to get a Grails 2.0 application to deploy to Weblogic 9. These occur because of Weblogic 9’s use of Java 1.5.
As an overview the 2 things you need to do are:
Change the XML schema declaration in the generated web.xml file to use Servlet specification 2.4 rather than 2.5
Put the JAXB jars in the lib folder of the project
Even a beginning object orientated developer probably knows that when possible you should favor Composition over Inheritance. Some people may also know this as the difference between Has-A (Composition) and Is-A (Inheritance). Annoyingly, for whatever reason, developers don’t seem to apply this principle when writing Java Swing code. Before we cover that let’s have a little composition vs. inheritance primer or recap whatever the case may be for your level of experience.
One of the things you frequently have to do is update the many side of a one-to-many relationship. Since Grails of course uses Hibernate the many side is a collection (a Set unless you change it) so when this relationship is updated you need to add new elements to the collection and then remove any element that should no longer be in the collection. Here I am going to show you an easy way to accomplish this. Note, that I will be showing this with Grails domain objects but the same thing will work just fine if you are using Hibernate directly.
I ran into a situation recently, while using a Groovy HTML builder, that took me a while to figure out. The Groovy HTML Builder is actually an XML builder so it will encode ampersands (&) as &. This is a problem when building links with parameters. This Groovy code and test case demonstrates the problem.
If you have worked with Swing you have surely heard yourself ask something like, “I set the preferred size of that component, why the hell isn’t it sizing it at that size, WTF???”. Swing layout and sizing is something that even experienced Java programmers suck at. I used to suck at it, until one day, years ago, I decided to figure out what the hell was going on. It turned out to be quite simple. There are just a few pieces of information you need to know to understand layout and sizing with Swing.
A few days ago I ran across a situation that was making me pull my hair out for half an afternoon. I was simply trying to iterate a JavaScript array to display some validation error messages that came back from the server as JSON. Easy, I thought, should be able to knock that out in about 2.2 seconds. Of course we know that isn’t true or I would have to change the name of the blog to The Good Programmer.
How do you get a MD5 hash in Java? Well I am glad you asked! I have somehow managed to create some Java code that does exactly that. Here it is for your cut and pasting pleasure! I have seen some code out there that won’t work correctly if the hashed value starts with zeros (generally because they are using a BigInteger). This version will properly display leading zeroes.