Tag: java

Updating log4j 1.x to 2.x

I’ve used Log4J 1.x for ages, and not even realized that the 1.x code line is not maintained any more, it seems all the activity is on 2.x as the latest maintained version of the framework.

To move from 1.x to 2.x, there’s a few changes:

If you’re using Maven for your dependencies, replace

<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
</dependency>

with

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.0.2</version>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.0.2</version>
</dependency>

The API has changed from:

org.apache.log4j.Logger

and

Logger.getLogger()

 

to

org.apache.logging.log4j.Logger

and

org.apache.logging.log4j.LogManager.getLogger()

 

Sample xml config – use filename log4j2.xml instead of log4j.xml (or log4j.properties):

<?xml version=”1.0″ encoding=”UTF-8″?>
<Configuration>
<Appenders>
<Console name=”STDOUT” target=”SYSTEM_OUT”>
<PatternLayout pattern=”%C – %m%n”/>
</Console>
</Appenders>
<Loggers>
<Logger name=”example.logger.name” level=”debug”/>
<Root level=”debug”>
<AppenderRef ref=”STDOUT”/>
</Root>
</Loggers>
</Configuration>

Additional useful info here.

(Page views: 166)

Adding a JMS Queue to Wildfly 8.x

The messaging subsystem in Wildfly is enabled in the standalone-full.xml config (not standalone.xml).

To add a new queue, search for <subsystem xmlns=”urn:jboss:domain:messaging:2.0″>, and then within the <hornetq-server> section, add a new <jms-destinations> if it doesn’t exist already, and define your queue name and JNDI lookup:

<jms-destinations>
    <jms-queue name="exampleQueue">
        <entry name="jms/queue/example"/>
        <entry name="java:jboss/exported/jms/queue/example"/>
    </jms-queue>
</jms-destinations>

 

(Page views: 206)

James Gosling at JavaOne 2014 vs JavaOne 2009

JavaOne 2014 wrapped up today, and was another great year, with plenty of awesome sessions. James Gosling played an active part in the Q&A during the Community Keynote this morning, and also gave a retrospective of the development of Java. He was wearing one of his Nighthacks Diner shirts, which I think we’re given out as a special prize at a JavaOne several years back (based on the painting by Edward Hopper, ‘Nighthawks Diner’). I seem to remember the design on this particular shirt, so did some digging in my photos from JavaOne conferences in the past, and here you go:

This is from JavaOne 2009 – I believe James was visiting some of the exhibitions in the Exhibition Hall:

 

And from this morning during the Community Keynote:

(Page views: 173)

Eclipse error: Access restriction: The type ‘xyz’ is not API

Eclipse has some pretty bizarre error messages that really don’t tell you exactly what the error is or how to fix it. This weekend I saw this one for example:

Access restriction: The type 'xyz' is not API (restriction on required library ...)

A quick Google told me what this actually means is that I have a line of code using a JDK API that is not in the currently selected runtime for the current project, but does exist in other available runtimes.

For example, when setting up a project with a Maven pom.xml, if you don’t explicitly specify what JVM version you want for the project, you get Java 5 by default.

There’s a couple of different ways to change the JVM version using Maven, but the approach I prefer is by adding properties (because it’s more concise than configuring the Maven compiler plugin):

<properties>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
</properties>

Alternatively if you’re not using Maven, just change the JRE System Library in the project settings on the Java Build Path/Libraries tab (remove the one that’s currently there and add the version that does have the APIs that you’re using, most likely a later version).

(Page views: 311)

Migrating Legacy AdMob to Google Play Services / Google Mobile Ads SDK

I developed my first Android app a few years back and at the time it was using Admob for mobile banner ads. Google bought AdMob a while back, and the time has come where Legacy AdMob usage is being retired this month (August 2014) and so you need to upgrade to the Google Play Services based Google Mobile Ads SDK.

First. Wow. I have to say, the steps and docs for how to do this seem to be spread over many different places. I’m not sure if all of these places actually walk you through the same steps just written in a different way, but it’s taken me a while to work out what I actually need to do. Some useful refs:

If you logon on the AdMob site, it will prompt you to complete a data migration step and update some account info – take care of that first here.

Info about legacy AdMod shutting down is here. Additional info in the FAQ.

Steps for moving to the Google Play Services based advertising seems to be covered here. I’m currently working through these steps for one of my apps, if I come across anything useful to share then I’ll post another update later.

(Page views: 222)

Java 7 & 8 install location on Mac OS X

Somewhere between Java 6 and 7 it seems I lost track of where your JDK gets installed on Mac OS X. Prior to Java 7, it seems it was installed to:

/System/Library/Frameworks/JavaVM.framework/Versions

with symlinks pointing to the exact locations.

I was just setting up a new Eclipse install and was looking for where my 8 was installed – it was clearly installed as ‘java -version’ was telling me I was running 8, but it was no longer in the above location.

/usr/libexec/java_home (which I’ve mentioned before here) was telling me the following:

/Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home

Hmm, so there you go. Looking in /Library/Java/JavaVirtualMachines/ I had multiple versions of 7 and 8. If you need to point Eclipse to a JRE location for your installed JREs, then from 7 onwards I think this is what you need.

(Page views: 237)

SSLProtocolException in SE7 & SE8: “handshake alert: unrecognized_name”

Caused by: com.sun.jersey.api.client.ClientHandlerException: 
javax.net.ssl.SSLProtocolException: handshake alert:  
unrecognized_name at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle (URLConnectionClientHandler.java:151) 
at com.sun.jersey.api.client.Client.handle(Client.java:648) 
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:680) 
at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74) 
at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:5 68) 

This exception is from an SSL check in SE7 and above that checks that your SSL certificate matches your domain name. For development, if you’re using a self-signed SSL certificate for testing which does not match your domain name, you can turn off the check and ignore the error with:

-Djsse.enableSNIExtension=false

(Page views: 384)

EE6 and EE7 Maven deps for JBoss AS7 and WildFly8

For EE7 on WildFly8.x, use:

Full profile:

        <dependency>
            <groupId>org.jboss.spec</groupId>
            <artifactId>jboss-javaee-7.0</artifactId>
            <version>1.0.0.Final</version>
            <type>pom</type>
            <scope>provided</scope>
        </dependency>

Web profile:

        <dependency>
            <groupId>org.jboss.spec</groupId>
            <artifactId>jboss-javaee-web-7.0</artifactId>
            <version>1.0.0.Final</version>
            <type>pom</type>
            <scope>provided</scope>            
        </dependency

For EE6 on JBoss AS7.x, use:

        <dependency>
            <groupId>org.jboss.spec</groupId>
            <artifactId>jboss-javaee-6.0</artifactId>
            <version>1.0.0.Final</version>
            <type>pom</type>
            <scope>provided</scope>
        </dependency>

Info from JBoss’s EE spec Git repo here and referenced from this question here.

(Page views: 159)

JSF 2.x Conditional error messages

To conditionally show a Twitter Bootstrap styled DIV with a list of error messages, use the rendered property on panelGroup to check whether messageList has messages to be displayed, and if so, render the enclosed content.

layout=”block” displays the output as a DIV, and row is one of the Bootstrap classes for row display.

<div class="span12 alert alert-danger"></div>

Set messages to display from your Backing Bean with:

FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("your message");

(Page views: 646)