Category Archives for Java

Change Font Size in Eclipse

​Here are some simple steps to change the font size in Eclipse.  You can also hit [ctrl][shift][++] to increase the font size but replaceing ++ with - - does not shrink the text.

Java Examples to Create JSON Arrays and JSON Objects

In the spirit of creating simple, copyable Java based JSON examples, here are some JSON Array and JSON Object processing samples. To keep it super easy to read, there is no database access or any other logic to clutter up the simple use cases presented here.

These examples are using the json-simple api that is common in many Java implementations.

Get the json-simple jar:  download json-simple-1.1.1.jar

Get the source for json-simple on github: download json-simple source

Check out the Google archive for more info on json-simple here: Read about it on Google

If you would like the eclipse project that contains the code below:  download java-json-simple eclipse project

Check out json.org for a long list of JSON parsers across many different languages.

 

This example creates a simple, single record style JSON Object.

 

This example creates an outer JSON object for “root” containing a JSON Array which contains 2 record style JSON Objects.

 

This example creates an outer JSON object for “root” containing a JSON Array which contains 2 record style JSON Objects.  Each of the record style JSON Objects contains a JSON Array.

Editing Files Outside of the Eclipse Workbench

Like a lot of people, I work with at least two editing tools in my day-to-day programming.  For Java I generally use Eclipse but for JavaScript I use vscode because I think it's lightyears ahead of Eclipse's JavaScript tooling.  Other people use other editors for their own reasons but if the files belong to an Eclipse workspace, you need to tell Eclipse to keep an eye out for external changes made to its own project files.  

Otherwise Eclipse will simply not see changes made by other editors.

Here is a link to the Eclipse documentation that deals with this.

In Eclipse, goto Preferences->General->Workspace 

On Windows, Window->Preferences->General->Workspace 

On Mac, Eclipse->Preferenes->General->Workspace

Check the box for "Refresh using native hooks or polling"

Java – Which JDK Version was Used to Compile

java.lang.UnsupportedClassVersionError

The above error means you have deployed code that was compiled from a more recent JDK than the JVM you are deploying to.

Which version of java built my jar or *.class file?

Maybe you have supporting jar files that contain code from Java 1.8 but you are deploying to java 1.5 – that is when you’ll see java.lang.UnsupportedClassVersionError.

Most of the time you will be aware of the JDK and JVM versions you are working with but occasionally you may have supporting code that will need to be recompiled at an earlier version. If you have lots of code, the question will be which code needs to be recompiled.

You can determine this by unzipping one class from each jar file and testing the version like so:

// on Windows run this
javap -v SomeJavaProgram.class
// on Mac, Linux, Unix, run this
javap -v SomeJavaProgram.class | grep major

If running from Windows, you will get a fairly large amount of output. You will find the text, “major version” within the first 10 lines.
If running from Mac, Linux, Unix, you will see something like this:
major version: 48

Regardless of OS, you can map the Major Version back to the JDK with this list:

45.3 = Java 1.1
46 = Java 1.2
47 = Java 1.3
48 = Java 1.4
49 = Java 5
50 = Java 6
51 = Java 7
52 = Java 8
53 = Java 9