I installed pi4j.deb and I I wanted to compile a code using this library but I got this error

Exception in thread "main" java.lang.NoClassDefFoundError: com/pi4j/wiringpi/Serial
at mainclass.MainClass.main(MainClass.java:19)
Caused by: java.lang.ClassNotFoundException: com.pi4j.wiringpi.Serial
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more

and you can see the code. I downloaded the library from "http://get.pi4j.com/download/pi4j-1.1.zip". would you help me finding it's problem.

//@author Arsalan

package mainclass;

import com.pi4j.wiringpi.Serial;

public class MainClass {

     * @param args the command line arguments
    public static void main(String[] args) {
        // TODO code application logic here
        int serialPort = Serial.serialOpen(Serial.DEFAULT_COM_PORT,9600);
        if(serialPort==-1) {
            System.out.println("Serial Failed!");
        else {
            System.out.println("serial OK!");
        Serial.serialPuts(serialPort, "Hey! I'm PI's Serial!!!");

  • What has your research found so far?
    – joan
    Mar 5, 2018 at 8:28
  • Looks like you need to put the location of the compiled pi4j stuff in $JAVALIBPATH.
    – goldilocks
    Mar 6, 2018 at 14:15

1 Answer 1


This is ultimately a question about Java that has nothing really to do with Raspberry Pi or pi4j. You need to provide the path to external libraries / sources when you compile and when you run. How you provide the path in Java depends on what type of resource you want to access. I don't know pi4j, but I'd guess that you need BOTH the path and the classpath to include appropriate values. The path to point to the native libraries that I assume underlie the implementation and the classpath to point to the jar file with the Java classes.

The path is typically set at the operating system level (or the shell level if you're in a linux-type system). The classpath can be set as an environment variable or passed as an option on the command line to the various Java commands (e.g. java and javac). You'll need to read the documentation to get a full handled on this.

See, for example, the PATH and CLASSPATH tutorial for Oracle.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.