Category Archives: Java

Do I have 32 or 64 bit Java Installed?

Hopefully by now you’re familiar with…

java -version

… to determine what version of Java you have installed, but sometimes you may want to know if you’re running a 32 bit or 64 bit version because you get an error like this…

Can't load AMD 64-bit .dll on a IA 32-bit platform

… to find out just run this variation of the command…

java -d64 -version

… if you have a 64 bit version you’ll get typical output. If not, you’ll get an error similar to this…

This Java instance does not support a 64-bit JVM.
Please install the desired version.
Please remember to subscribe to the newsletter to stay up to date!

You or someone you know looking to buy or sell?
Disclaimer: Thoughts and opinions are my own, and do not reflect the views of any employer, family member, friend, or anyone else. Some links may be affiliate links, but I don't link to anything I don't use myself. You would think this should be self evident these days, but apparently not...

NullPointerException – could not publish to server

Ok, so this is an obscure one, but if it saves anyone from multiple days of banging their head against the wall it’s worth it…

Recently I was working in eclipse, had created my component, compiled it, and had it pushed to our ivy repository. I then went to bring it into another (web) project when I started getting complaints about a NullPointerException. For kicks I tried running the web app and received the error “NullPointerException (could not publish to server)”. With all that useful information how could I not figure out what the problem was?

Fortunately another developer I spoke with had seen the issue before and pointed me in the right direction. In the utility component/jar you have to (see screen shot below too)…

  1. Go to Project -> Properties
  2. Click on “Project Facets”
  3. Check “Utility Module”

And then it will just magically work. Wasn’t that completely obvious?

EclipseUtilityMode

Please remember to subscribe to the newsletter to stay up to date!

You or someone you know looking to buy or sell?
Disclaimer: Thoughts and opinions are my own, and do not reflect the views of any employer, family member, friend, or anyone else. Some links may be affiliate links, but I don't link to anything I don't use myself. You would think this should be self evident these days, but apparently not...

Determine Default Java JVM HeapSize

In the life of a Java developer every now and then you’ll have one of your apps run out of memory. While this is usually due to poor coding (not closing resources, or managing large files) it helps to know exactly what the defaults are for your system. I had just this need today so went out looking for an answer.

What I found that was the most helpful was this page, but for quick reference here’s how to do it on Windows…

c:\>java -XX:+PrintFlagsFinal -version | findstr /i "HeapSize PermSize ThreadStackSize"
 
    uintx InitialHeapSize                          := 266634176       {product}
    uintx MaxHeapSize                              := 4267704320      {product}
    uintx PermSize                                  = 21757952        {pd product}
    uintx MaxPermSize                               = 85983232        {pd product}
     intx ThreadStackSize                           = 0               {pd product}
java version "1.7.0_40"
Java(TM) SE Runtime Environment (build 1.7.0_40-b43)
Java HotSpot(TM) 64-Bit Server VM (build 24.0-b56, mixed mode)

Note that the values returned are in bytes so throw them into google to convert them into something useful 🙂

Please remember to subscribe to the newsletter to stay up to date!

You or someone you know looking to buy or sell?
Disclaimer: Thoughts and opinions are my own, and do not reflect the views of any employer, family member, friend, or anyone else. Some links may be affiliate links, but I don't link to anything I don't use myself. You would think this should be self evident these days, but apparently not...

Analyzing a Memory Leak in a Java Application

In trying to diagnose an Out of Memory error in a large Java application, a co-worker of mine (Chad Handrich) came across the article Analyzing a Memory Leak in Java Applications using VisualVM and used the tips in it to find a memory leak that had been causing OOM errors for quite some time.

He also found that another helpful tool is the “Performance Monitor” app that comes with Windows. To use it, create a new “User Defined Data Collector Set”. Choose the option “Create Manually”, then “Performance Counter”, then you can select “Process”, and choose all the byte-count style counters if analyzing memory usage. And choose the “java.exe” instance from the list. Once it’s created, right click the collector you made and select “Start”. It will begin polling and saving the metrics you specified.

Please remember to subscribe to the newsletter to stay up to date!

You or someone you know looking to buy or sell?
Disclaimer: Thoughts and opinions are my own, and do not reflect the views of any employer, family member, friend, or anyone else. Some links may be affiliate links, but I don't link to anything I don't use myself. You would think this should be self evident these days, but apparently not...

How do I override the JVM Heap Size?

If you’ve ever received a java.lang.OutOfMemoryError when processing a large amount of data, there’s a chance you need to manually specify the JVM size if you can. Here’s an example…

java -Xms64m -Xmx256m MyProgram

… Of course, another option you may want to consider is to structure your program more efficiently (i.e. don’t store all that data in memory, process in blocks, use a tool more suitable to the task like BCP or DB2 LOAD).

Want to read more on the subject? Read this

Please remember to subscribe to the newsletter to stay up to date!

You or someone you know looking to buy or sell?
Disclaimer: Thoughts and opinions are my own, and do not reflect the views of any employer, family member, friend, or anyone else. Some links may be affiliate links, but I don't link to anything I don't use myself. You would think this should be self evident these days, but apparently not...

Resolving Ant javac “class file has wrong version” issues

Ever run an ANT build in eclipse and it works just fine, but as soon as you run it outside of eclipse you get something like…

class file has wrong version 51.0, should be 49.0

The issue is likely that you need to “fork” in your javac task…

Before (with error)

javac srcdir="${build.path}" 
   verbose="true" 
   includeantruntime="false" 
   includejavaruntime="false"

After (no error)

javac srcdir="${build.path}" 
   verbose="true" 
   includeantruntime="false" 
   includejavaruntime="false" 
   fork="yes"

Hopefully this can save you about 2 hours some day!

Please remember to subscribe to the newsletter to stay up to date!

You or someone you know looking to buy or sell?
Disclaimer: Thoughts and opinions are my own, and do not reflect the views of any employer, family member, friend, or anyone else. Some links may be affiliate links, but I don't link to anything I don't use myself. You would think this should be self evident these days, but apparently not...

New cffreedom-utils repo on github

For the last 10 years or so I’ve built up a number of Java utility functions and classes to help me write code easier. Over time I’ve probably had to re-write many of the functions as I’ve moved teams, lost the code, or just wanted to make it better. I’ve also had to jar up and help other developers use the code one too many times. Since I’m finally sick of that I’ve switch over to Maven for dependency and build management, moved the library to github, and am open sourcing the development so that others can take advantage of my work, and contribute to it to make it better.

You can find the project at https://github.com/communicationfreedom/cffreedom-utils. Please take a look, and consider helping make it even better.

Note: Since I’ve been using the project more and more the pace of change is pretty steep and the package structure has been changing frequently, but I believe that is starting to settle down a bit.

Please remember to subscribe to the newsletter to stay up to date!

You or someone you know looking to buy or sell?
Disclaimer: Thoughts and opinions are my own, and do not reflect the views of any employer, family member, friend, or anyone else. Some links may be affiliate links, but I don't link to anything I don't use myself. You would think this should be self evident these days, but apparently not...

XPath Query Not Returning any Results in Java

In looking over documentation on XPath queries it seemed easy enough to be able to pull out values from XML, but try as I might I kept getting no results back. Even searching on the root element “/elementName” didn’t return any results. Of course, as with all things Java there’s a trick and that trick is to call setNamespaceAware(). Here’s a quick and dirty example…


DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(false); // NEVER FORGET THIS
DocumentBuilder builder = domFactory.newDocumentBuilder();
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile("/items/item[@id='2']");
return (NodeList)expr.evaluate(domDocument, XPathConstants.NODESET);
Please remember to subscribe to the newsletter to stay up to date!

You or someone you know looking to buy or sell?
Disclaimer: Thoughts and opinions are my own, and do not reflect the views of any employer, family member, friend, or anyone else. Some links may be affiliate links, but I don't link to anything I don't use myself. You would think this should be self evident these days, but apparently not...

Output a DOM Document as XML using Java

Ok, here’s another nerdy post for all the java developers out there.  Hopefully it saves you some time.

Let’s say you have a XML DOM Document that you’ve been modifying in your code, and now want to write it back out as XML.  One would think there would be a nice toXml() method or something on the Document, but there isn’t.  Instead, make sure you have the xalan.jar file on your class path and use this function…


public static String getDomDocumentAsXml(org.w3c.dom.Document domDocument)
{
	try
	{
		TransformerFactory tf = TransformerFactory.newInstance();
		Transformer transformer = tf.newTransformer();
		transformer.setOutputProperty(OutputKeys.INDENT, "yes");
		StreamResult xmlOutput = new StreamResult(new StringWriter());
		transformer.transform(new DOMSource(domDocument), xmlOutput);
		return xmlOutput.getWriter().toString();
	}
	catch (Exception e) { e.printStackTrace(); return null; }
}
Please remember to subscribe to the newsletter to stay up to date!

You or someone you know looking to buy or sell?
Disclaimer: Thoughts and opinions are my own, and do not reflect the views of any employer, family member, friend, or anyone else. Some links may be affiliate links, but I don't link to anything I don't use myself. You would think this should be self evident these days, but apparently not...

Add a SSL Cert to Your Java Keystore

If you’ve ever had to add a SSL certificate to a java keystore, you know that the command is a little convoluted.  Here for your and my reference is the command…

 "D:\Program Files\Java\jre1.5.0_22\bin\keytool" -import -trustcacerts -alias MyCA2 -file C:\MyCA2.crt -keystore "D:\Program Files\Java\jre1.5.0_22\lib\security\cacerts" 

When promoted for the password, the default is “changeit”, and make sure to choose/type “yes” when asked if you want to trust the cert.

Please remember to subscribe to the newsletter to stay up to date!

You or someone you know looking to buy or sell?
Disclaimer: Thoughts and opinions are my own, and do not reflect the views of any employer, family member, friend, or anyone else. Some links may be affiliate links, but I don't link to anything I don't use myself. You would think this should be self evident these days, but apparently not...