Validating JVM Arguments in Code
20-Nov-08
I can’t think of any real uses for this right now, but I can think of lots of possible uses!
Frank Carver’s musings about software and life
I can’t think of any real uses for this right now, but I can think of lots of possible uses!
My personal preference is almost always to deal with a database at a fairly direct level. I have built up a bunch of Java code which largely removes the pain of database access, but it is certainly not any kind of ORM (Object-Relational Mapping) tool. I rely on understanding both the structure and efficiency of the database, and the clarity and effectiveness of the code.
This article recounts one development team’s problems with using too high a level of abstraction for data persistence, and forgetting that there is a database at the bottom of their pile of software.
No New Ideas: ActiveRecord lessons learnt: #1 Never forget there’s a database
Up until recently, I was using a Wordpress plugin called “WP-codebox” to format my code samples in this blog, but today it broke my RSS feed, so I have abandoned it. I liked the “geshi” syntax highlighting engine used inside it, but unfortunately it generated flaky HTML, and was not smart enough to disable JavaScript in feeds.
I have now replaced it with WP-syntax. using it is very similar, just wrap code examples in (for example)
<pre lang="Java">
class Whatever {
public void thing() {
if(thisHappens()) {
doThis();
doThat();
}
}
}
</pre>
gives
class Whatever { public void thing() { if(thisHappens()) { doThis(); doThat(); } } }
Please let me know if you have any problems with the appearance of code samples, especially if you use a feed reader or aggegator.
How’s this for an extreme opinion:
If you use an ‘if’ you deserve to suffer at Mark Needham
I actually agree with some of his conclusions, although I am surprised that he does not suggest the simple approach of extracting a method to eliminate the potential confusion of multiple statements depending on an “if”. For example, replace:
class Whatever { public void thing() { if(thisHappens()) { doThis(); doThat(); } } }
with
class Whatever { private void thisAndThat() { doThis(); doThat(); } public void thing() { if (thisHappens()) thisAndThat(); } }
Seems less trouble than a forced polymorphism approach, at least until polymorphism and the strategy pattern is actually needed.
It’s a long-running argument. Are modern, dynamic, languages such as Ruby and Python really much more concise than more mainstream languages such as Java? What constitutes “big” in each case? Stephan tries to flush out some hard facts, and gets a lot of comments.
I started the day cursing the stupid jar format, but ended the day smiling. I needed to build a stand-alone executable jar which accessed a MySQL database but kept getting all sorts of build-time and run-time errors.
Normally to make an executable jar I use the excellent pack ant task which build a minimal jar file by trawling all the class dependencies in yout code and including just the required classes from the classpath. For example:
<target name="jar"> <taskdef name="pack" classname="org.sadun.util.ant.Pack" classpath="lib/pack.jar" /> <pack classes = "${root.classes}" targetJar = "dist/${project.name}.jar" manifestMainClass = "${main.class}" excludePkg = "java,sun" includePkg = "com,org,javax" > <classpath refid="classpath"/> </pack> </target>
Just set up the main.class property to the main class of the application, and set the root.classes property to a class which refers to everything you need (in many cases this will be the same main class) and you get a lovely small executable jar.
However, trying this approach with MySQL cause a bunch of problems. First I needed to add a load of (apparently dynamically loaded) MySQL class names such as com.mysql.jdbc.Driver.
I thought I was doing well when I had resolved that issue, but then I hit an even harder problem:
java.lang.RuntimeException: Can’t load resource bundle due to underlying exception
java.util.MissingResourceException: Can’t find bundle for base name com.mysql.jdbc.LocalizedErrorMessages, locale en_GB
Despite spending an hour or so searching and trying, I could not convince pack to find it. So I had to look elsewhere.
The solution I eventually chose was “one-jar” - a trick which subverts the normal jar execution process and creates a custom classloader to resolve jars within a jar. I still use pack to minimise the amount of other classes included in my project, but explicitly include the whole MySQL driver jar.
My ant target now looks like:
<target name="jar"> <taskdef name="pack" classname="org.sadun.util.ant.Pack" classpath="lib/pack.jar" /> <pack classes = "${root.classes}" targetJar = "tmp/main.jar" manifestMainClass = "${main.class}" excludePkg = "java,sun" includePkg = "com,org,javax" > <classpath refid="classpath"/> </pack> <taskdef name="one-jar" classname="com.simontuffs.onejar.ant.OneJarTask" classpath="lib/one-jar-ant-task-0.96.jar" onerror="report"/> <one-jar destfile="dist/${project.name}.jar" manifest="src/main/files/${project.name}.mf"> <main jar="tmp/main.jar"/> <lib> <fileset file="lib/mysql-connector-java-5.0.4-bin.jar"/> </lib> </one-jar> </target>
This builds all of my application except the MySQL bits using pack into a temporary “main.jar”, then uses one-jar to build another jar which refers to both main.jar and the MySQL driver jar.
I did need to create an explicit manifest file this way (usually, pack is smart enough to make it for me)
Main-Class: com.simontuffs.onejar.Boot One-Jar-Main-Class: org.example.project.Main
I now have a working stand-alone executable jar which can access a MySQL database. Cool.
Read more at Deliver Your Java Application in One-JAR™ ! and Java: Using ONE-JAR
Scott Selikoff recently posted that we should “never return null arrays”. I’m not sure I entirely agree. Both the tone of the article and the comments so far seem to be in agreement, but I am still not so sure.
I’ll skip the terminology issue for the moment, just note that an array in Java is different, in syntax, interface and implementation, from a Collection. Scott’s article seems to discuss Collections rather than arrays.
As far as I am concerned, there is a qualitative distinction between a Collection which exists, but is empty, and a collection which simply does not exist (i.e is null). I’ll grant that in many situations they should result in the same action or output; but not always.
Imagine parsing XML documents. Consider the following three cases:
<result> </result>
<result> <stuff/> </result>
<result> <stuff> <item id="1"/> <item i