JavaPNS NoClassDefFoundError

799 Views Asked by At

I am using JavaPNS to send remote push notifications (iOS) via my home computer. After following the steps on raywenderlich.com on how to prepare the certificates, I took my .p12 file and placed it in the code written below:

  import javapns.Push;
  import javapns.notification.Payload;
  import javapns.notification.PushedNotifications;

  import org.apache.log4j.*;

  public class Main{
        public static void main(String[] args) {
              Push.alert("Hello World!", "PushChatKey.p12", "pushchat", false, "Token");
        }

  }

I didn't include my device token in the code above, but I am 100% sure that the token number in my actual program is the correct one for my device. When I run the program, I get the following error in the (eclipse) console:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
    at javapns.notification.Payload.<clinit>(Payload.java:25)
    at javapns.Push.alert(Push.java:47)
    at Main.check(Main.java:86)
    at Main.main(Main.java:115)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 4 more

My .p12 file is located inside my java project folder, and upon reviewing other previously asked questions, I put the org.apache import statement inside my program.

Other answers I've seen to questions like this is to use a try-catch statement containing the line: BasicConfigurator.configure();

When I placed this in, Eclipse told me: "BasicConfigurator cannot be resolved" and it gives me the option to create the class myself.

I'm not sure what I am doing wrong. Any help would be greatly appreciated. Thanks in advance to all who reply.

1

There are 1 best solutions below

0
On

It is because javapns is using the dependency log4j to log information and that it can't find it at run-time.

Javapns wiki documentation is stating here :

Enable logging Library logging

javapns uses Log4J for logging. To enable logging quickly, add the following code:

 import org.apache.log4j.*;

and before using javapns in your code:

try {

    BasicConfigurator.configure();

 } catch (Exception e) {  }

If you are already using log4j, you can enable logging for javapns by adding the following line to your log4j.properties file:

log4j.logger.javapns=debug

Java SSL logging To troubleshoot certificate or SSL issues, add the following to your java VM arguments:

-Djavax.net.debug=all

If you are using Maven then add this dependency to your actual dependencies :

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

If you are not using Maven then just download the .jar and add it to your project's libs.

On my side, I just can't add log4j to my dependencies because it makes one of my other libs crash. I'm actually looking for a way to disable log4j for javapns.

Edit: I ended up adding log4j to my dependencies and adding BasicConfigurator.configure(); to my jersey's ResourceConfig class. I was probably missing this part when using log4j with my other lib so now it works just fine.