Squidward
Posts: 2
Joined: Mon Nov 19, 2012 11:35 am

pi4j and $CLASSPATH

Mon Nov 19, 2012 12:01 pm

Hi ,

Bit of a noob to java. What is the correct way to compile and run a program using pi4j ? What am I doing wrong in the following example ? Many thanks.

Code: Select all

[email protected]:~/java$ ls
pi4j-core-0.0.3-SNAPSHOT.jar  Test.java
[email protected]:~/java$
[email protected]:~/java$ cat Test.java 

import com.pi4j.io.gpio.*;

public class Test
{
    Test () {
    GpioController gpio = GpioFactory.getInstance();
    }

    public static void main ( String [] args ) {
    Test t = new Test ();
    }
}

[email protected]:~/java$ javac Test.java 
Test.java:2: error: package com.pi4j.io.gpio does not exist
import com.pi4j.io.gpio.*;
^
Test.java:7: error: cannot find symbol
	     GpioController gpio = GpioFactory.getInstance();
	     ^
  symbol:   class GpioController
  location: class Test
Test.java:7: error: cannot find symbol
	     GpioController gpio = GpioFactory.getInstance();
	                           ^
  symbol:   variable GpioFactory
  location: class Test
3 errors
[email protected]:~/java$ javac -cp ./pi4j-core-0.0.3-SNAPSHOT.jar  Test.java 
[email protected]:~/java$ java Test 
Exception in thread "main" java.lang.NoClassDefFoundError: com/pi4j/io/gpio/GpioFactory
	  at Test.<init>(Test.java:7)
	  at Test.main(Test.java:11)
Caused by: java.lang.ClassNotFoundException: com.pi4j.io.gpio.GpioFactory
       at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
       at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
       at java.security.AccessController.doPrivileged(Native Method)
       at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
       at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
       at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
       at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
       ... 2 more
[email protected]:~/java$ java -cp ./pi4j-core-0.0.3-SNAPSHOT.jar Test 
Error: Could not find or load main class Test
[email protected]:~/java$ 

User avatar
xranby
Posts: 539
Joined: Sat Mar 03, 2012 10:02 pm
Contact: Website

Re: pi4j and $CLASSPATH

Mon Nov 19, 2012 1:26 pm

To run it, remember to add the current directory to the classpath where you keep the Test.class, first add a : to indicate that you want to add one more path and then a . that equals the current directory!

Code: Select all

java -cp ./pi4j-core-0.0.3-SNAPSHOT.jar:.  Test
Xerxes Rånby @xranby I once had two, then I gave one away. Now both are in use every day!
twitter.com/xranby

Squidward
Posts: 2
Joined: Mon Nov 19, 2012 11:35 am

Re: pi4j and $CLASSPATH

Mon Nov 19, 2012 4:22 pm

Excellent !

Thanks.

User avatar
savageautomate
Posts: 225
Joined: Thu Aug 16, 2012 3:20 pm
Location: USA
Contact: Website

Re: pi4j and $CLASSPATH

Thu Nov 22, 2012 2:53 am

Also make sure to use the "sudo" command in front of the "java" command to start your java program with super user rights. Pi4J will need root permissions to performs some of its tasks.

Here is a complete example including the command lines to compile and execute the example code.
http://pi4j.com/example/control.html


Thanks, Robert
Robert Savage | Follow me @savageautomate
http://www.pi4j.com | http://www.pislices.com
http://www.savagehomeautomation.com

angelomastro
Posts: 1
Joined: Tue Sep 03, 2019 12:07 pm

Re: pi4j and $CLASSPATH

Tue Sep 03, 2019 12:17 pm

Hi guys, this is happening to me too. but I think I am doing the right thing you said.

Test.java:

Code: Select all

import com.pi4j.wiringpi; 

public class Test {
   public static void main(String[] args) throws InterruptedException {
      if (Gpio.wiringPiSetup() == -1) {
          System.out.println(" ==>> GPIO SETUP FAILED");
          throw new Exception("Gpio.wiringPiSetup() failed");
       }       
       System.out.println("    > init pints in BOARD mode");
       wiringpi.Gpio.piBoardRev();
       wiringpi.Gpio.pinMode(7, wiringpi.Gpio.OUTPUT);
       wiringpi.Gpio.pinMode(11, wiringpi.Gpio.OUTPUT);
       wiringpi.Gpio.pinMode(13, wiringpi.Gpio.OUTPUT);
       wiringpi.Gpio.pinMode(15, wiringpi.Gpio.OUTPUT);
       
       System.out.println("    > move forward");
       wiringpi.Gpio.digitalWrite(7, wiringpi.Gpio.LOW);
       wiringpi.Gpio.digitalWrite(11, wiringpi.Gpio.HIGH);
       wiringpi.Gpio.digitalWrite(13, wiringpi.Gpio.LOW);
       wiringpi.Gpio.digitalWrite(15, wiringpi.Gpio.HIGH);
      
        // etc etc ... other operations  
   }
}
Then I compile with :

Code: Select all

javac -cp classes:libs/* -d classes /path/to/Testjava
where "classes" contains the classpath and output class path (always works in other projects), and libs contains the jar file to use, for intsance pi4j-core.jar. Note: the * character is not an issue, I can point -cp directly to the jar and the same thing happens.

And in return I get

Code: Select all

/path/to/Test.java:xx: error: package com.pi4j does not exist
import com.pi4j.wiringpi;
               ^
/path/to/Test.java:xx: error: package wiringpi does not exist
    if (wiringpi.Gpio.wiringPiSetup() == -1) {
                ^
// ... etc etc for all commands

Return to “Other programming languages”