jonny maguire
jonny maguire

Reputation: 13

Exception in thread "main" java.lang.NoClassDefFoundError: com/pi4j/io/gpio/GpioFactory

getting these errors when trying to run my java program

Exception in thread "main" java.lang.NoClassDefFoundError: com/pi4j/io/gpio/GpioFactory
        at FileWatch.main(FileWatch.java:53)
Caused by: java.lang.ClassNotFoundException: com.pi4j.io.gpio.GpioFactory
        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)
        ... 1 more

and heres my code,

public class FileWatch {

        static String clkID;
        static String clkID2;
        static String ts;
        static String ts1;
        static boolean done = false;
        static boolean REdone = false;
        static boolean finished = false;
        static boolean ready;

        static String host ="jdbc:mysql://localhost/dancers";
        static String username ="root";
        static String password ="beaker19";


        public static void main(String[] args) throws IOException,
                        InterruptedException {

       // create gpio controller
        final GpioController gpio = GpioFactory.getInstance();

        // provision gpio pin #01 as an output pin and turn on
        final GpioPinDigitalOutput pin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_01, "MyLED", PinState.HIGH);


                while (true) {
                        done = false;
                        REdone=false;
                        checkFile();
                        pin.high();
                        Thread.sleep(3000);
                        REcheckFile();
                        Thread.sleep(500);
                        //Thread.
                        if (clkID.equals(clkID2)) {

                        uploadTimes();

                        }

                        else {
                                System.out.println("Wrong matching ID's");
                        }
                        Thread.sleep(1000);
                        pin.low();
                 }
}

I've never worked with pi4j so i dont know where i am going wrong!! it seems to be a problem with gpiofactory class but i havent made any changes to it and not to sure whats going from what i can read on the internet its a problem with the package being imported...

i have compiled like so:

pi@raspberrypi ~/rpi2ardu $ javac -cp .:mysql-connector-java-3.1.1.jar:/opt/pi4j/lib/'*' FileWatch.java

Upvotes: 0

Views: 3778

Answers (4)

NELSON RODRIGUEZ
NELSON RODRIGUEZ

Reputation: 379

Add lib path command -Djava.ext.dirs=/path to execture command, they points to pi4j-core jar

example:

sudo java -Djava.ext.dirs=/"path to pi4j-core jar" -jar "/path to jar"

Upvotes: 0

tommybee
tommybee

Reputation: 2579

Do not use '*'

-cp .:mysql-connector-java-3.1.1.jar:/opt/pi4j/lib/'*'

Just use it as follows,

-cp .:mysql-connector-java-3.1.1.jar:/opt/pi4j/lib

or

export CLASSPATH=mysql-connector-java-3.1.1.jar:/opt/pi4j/lib/pi4j-core.jar:/opt/pi4j/lib/pi4j-device.jar:/opt/pi4j/lib/pi4j-gpio-extension.jar:/opt/pi4j/lib/pi4j-service.jar:$CLASSPATH

for future use.

Upvotes: 0

CYBORGCNC
CYBORGCNC

Reputation: 45

I solved the same exact issue.

Find your app Manifest file. One is created by default and is under your source tree =>META-INF=>MANIFEST.MF

Simply, add to it a class path, to where the pi4j JAR files are, it should look like this:

{        
    Manifest-Version: 1.0
    Main-Class: yourclassname.Main
    Class-Path: /opt/pi4j/lib/pi4j-core.jar /opt/pi4j/lib/pi4j-device.jar /opt/pi4j/lib/pi4j-gpio-extension.jar /opt/pi4j/lib/pi4j-service.jar
}

Build your file, execute it.

Upvotes: 1

user6009923
user6009923

Reputation: 1

I had a similar situation and the link below helped me. What I forgot to do was add the current working directory in the class path.

"." is for the current working directory

example on Raspbian

java -classpath .:/opt/pi4j/lib/'*' FileWatch

link that had the answer -https://www.raspberrypi.org/forums/viewtopic.php?f=34&t=23239

Upvotes: 0

Related Questions