JBBoichat
Posts: 1
Joined: Sat Jul 06, 2019 12:27 pm

Executing java with sudo not working for a Relay on Raspberry Pi 4

Mon Jul 08, 2019 7:11 am

Hi all,

Code: Select all

java -classpath .:/opt/pi4j/lib/'*' Relay
works perfectly on Raspberry Pi 3 or Pi 4 as well as the associated Python script for verification.
I use the GPIO 4 (Pi4J numbering, the physical pin 16).

However, if I use sudo

Code: Select all

sudo java -classpath .:/opt/pi4j/lib/'*' Relay
the relay is not activated.

The code use high() and low() of an object of the class Pin.

Strange! Isn't it!

I installed my new Raspberry Pi 4 using 2019-06-20-raspbian-buster-full.img and the empty ssh and wpa_supplicant.conf procedure to setup the WiFi.
Then I installed the Pi4J using curl -s get.pi4j.com | sudo bash.

All my Raspberry Pi 3 (I have all types) are used without any screen or keyboard.
My tools: PuTTY, WinSCP and Eclipse on Windows PC for the Java code compilation.

The main application I am using for verifying my new Pi 4 is a presence simulator written in Java.
All my components are first tested with Python scripts and then, for each class, I have a main entry for testing the class and the or theirs component(s).

Actually, I noticed that for my Raspberry Pi applications, I use sometimes java and sometimes sudo java: I could not actually remember why.

Before moving to the Raspberry Pi 4 my Eclipse JDK was configured to use Java 8 as the Pi4J library.
Now, on Buster, we have Java 11 and I also tried to compile with the JDK 11 on the PC, and this does bring any difference (seems to be logic).

The sudo java should have more rights to execute my class Relay. Isn't it? Strange ... anyway!

Before removing the sudo, I suspected the power supply: I used an old Pi 3 model instead of the 15W recommended. Right now my Pi 4 is powered from my PC USB 3: great!

I didn't try yet any other components (leds, PIR, ....).

Thanks to all

petr2
Posts: 9
Joined: Mon Apr 17, 2017 11:05 am

Re: Executing java with sudo not working for a Relay on Raspberry Pi 4

Sat Nov 16, 2019 6:17 am

The general issue is that using sudo changes the environment variables. First, try "sudo -E java ..." to preserve the environment - that will test it.

As there is no known way apparently [https://askubuntu.com/questions/161924/ ... s-for-root] to set the classpath for root, you may want to do
sudo visudo /etc/sudoers
and put a '!' before "env_reset"

note you'll be using vi as the editor so you may want to practice on a less critical file first.
p

Return to “Java”